Skip to content

Commit 18b4e7a

Browse files
committed
feat: handle error responses in Azure OpenAI adapter by yielding raw response on status code >= 400
1 parent 7fca704 commit 18b4e7a

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

api/app/services/protocols/openai_responses.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ async def openai_chat_sse_to_responses_sse(stream: AsyncIterator[bytes], *, mode
137137
prompt_tokens = 0
138138
completion_tokens = 0
139139
started = False
140+
text_parts: list[str] = []
140141
async for obj in _iter_sse_json(stream):
141142
usage = obj.get("usage") or {}
142143
if usage:
@@ -146,6 +147,7 @@ async def openai_chat_sse_to_responses_sse(stream: AsyncIterator[bytes], *, mode
146147
delta = choice.get("delta") or {}
147148
text = delta.get("content")
148149
if text:
150+
text_parts.append(str(text))
149151
if not started:
150152
started = True
151153
yield _sse("response.created", {"type": "response.created", "response": _response_stub(response_id, model, "in_progress")})
@@ -157,10 +159,15 @@ async def openai_chat_sse_to_responses_sse(stream: AsyncIterator[bytes], *, mode
157159
yield _sse("response.created", {"type": "response.created", "response": _response_stub(response_id, model, "in_progress")})
158160
yield _sse("response.output_item.added", {"type": "response.output_item.added", "output_index": 0, "item": {"id": item_id, "type": "message", "status": "in_progress", "role": "assistant", "content": []}})
159161
yield _sse("response.content_part.added", {"type": "response.content_part.added", "item_id": item_id, "output_index": 0, "content_index": content_index, "part": {"type": "output_text", "text": "", "annotations": []}})
160-
yield _sse("response.output_text.done", {"type": "response.output_text.done", "item_id": item_id, "output_index": 0, "content_index": content_index, "text": ""})
161-
yield _sse("response.content_part.done", {"type": "response.content_part.done", "item_id": item_id, "output_index": 0, "content_index": content_index, "part": {"type": "output_text", "text": "", "annotations": []}})
162-
yield _sse("response.output_item.done", {"type": "response.output_item.done", "output_index": 0, "item": {"id": item_id, "type": "message", "status": "completed", "role": "assistant", "content": []}})
162+
output_text = "".join(text_parts)
163+
content = {"type": "output_text", "text": output_text, "annotations": []}
164+
item = {"id": item_id, "type": "message", "status": "completed", "role": "assistant", "content": [content]}
165+
yield _sse("response.output_text.done", {"type": "response.output_text.done", "item_id": item_id, "output_index": 0, "content_index": content_index, "text": output_text})
166+
yield _sse("response.content_part.done", {"type": "response.content_part.done", "item_id": item_id, "output_index": 0, "content_index": content_index, "part": content})
167+
yield _sse("response.output_item.done", {"type": "response.output_item.done", "output_index": 0, "item": item})
163168
completed = _response_stub(response_id, model, "completed")
169+
completed["output"] = [item]
170+
completed["output_text"] = output_text
164171
completed["usage"] = {"input_tokens": prompt_tokens, "output_tokens": completion_tokens, "total_tokens": prompt_tokens + completion_tokens}
165172
yield _sse("response.completed", {"type": "response.completed", "response": completed})
166173
yield b"data: [DONE]\n\n"

api/app/services/providers/azure_openai.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ async def responses_from_chat(self, channel: Channel, upstream_model: str, paylo
203203
async def gen() -> AsyncIterator[bytes]:
204204
async with httpx.AsyncClient(timeout=None) as cli:
205205
async with cli.stream("POST", url, json=body, headers=headers) as r:
206+
if r.status_code >= 400:
207+
raw = await r.aread()
208+
yield raw
209+
return
206210
async for chunk in responses_sse_to_openai_chat_sse(r.aiter_raw(), model=upstream_model):
207211
yield chunk
208212

0 commit comments

Comments
 (0)