fix: connect directly via websockets to bypass OpenAI-Beta header
The openai library's beta.realtime.connect() hardcodes the obsolete 'OpenAI-Beta: realtime=v1' header which the GA API rejects. Connecting directly via the websockets library with only the Authorization header resolves the 'beta_api_shape_disabled' error.
This commit is contained in:
@@ -58,12 +58,22 @@ class HybridPipeline:
|
|||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
import websockets
|
||||||
|
|
||||||
self._openai = AsyncOpenAI(api_key=settings.openai_api_key)
|
self._openai = AsyncOpenAI(api_key=settings.openai_api_key)
|
||||||
|
|
||||||
logger.info("Connecting to gpt-realtime-whisper...")
|
logger.info("Connecting to gpt-realtime-whisper...")
|
||||||
async with self._openai.beta.realtime.connect(
|
|
||||||
model="gpt-realtime-whisper",
|
# Connect directly via websockets to avoid the OpenAI-Beta header
|
||||||
) as conn:
|
url = f"wss://api.openai.com/v1/realtime?model=gpt-realtime-whisper"
|
||||||
|
ws = await websockets.connect(
|
||||||
|
url,
|
||||||
|
additional_headers={
|
||||||
|
"Authorization": f"Bearer {settings.openai_api_key}",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
async with ws as conn:
|
||||||
self._conn = conn
|
self._conn = conn
|
||||||
self._connected = True
|
self._connected = True
|
||||||
logger.info("Connected to gpt-realtime-whisper")
|
logger.info("Connected to gpt-realtime-whisper")
|
||||||
@@ -90,8 +100,10 @@ class HybridPipeline:
|
|||||||
# Listen for transcription events
|
# Listen for transcription events
|
||||||
while self._connected:
|
while self._connected:
|
||||||
try:
|
try:
|
||||||
event = await conn.recv()
|
raw = await conn.recv()
|
||||||
await self._handle_event(event)
|
if isinstance(raw, (str, bytes)):
|
||||||
|
data = json.loads(raw if isinstance(raw, str) else raw.decode())
|
||||||
|
await self._handle_event(data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if self._connected:
|
if self._connected:
|
||||||
logger.warning(f"recv error: {e}")
|
logger.warning(f"recv error: {e}")
|
||||||
@@ -201,7 +213,7 @@ class HybridPipeline:
|
|||||||
|
|
||||||
async def _send(self, data: dict):
|
async def _send(self, data: dict):
|
||||||
try:
|
try:
|
||||||
await self._conn.send(data)
|
await self._conn.send(json.dumps(data))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Send error: {e}")
|
logger.warning(f"Send error: {e}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user