Repository navigation
Description
Problem Description
LLMClient.chat_stream through the gateway does not read the status of an error frame. After a keepalive comment the gateway has sent its status line, so LiteLLM writes its error reply into an SSE frame whose code is the status (_frame_status in src/devops_cli/ai/client/openai.py). chat maps that frame through _status_error, so it gives the status, a credentials error for 401 or 403, and the spend-limit error of a spent provider key. chat_stream does not: the frame escapes as a raw openai.APIError, or becomes Cannot connect to AI provider API (APIError: ...).
Reproduction Steps
- Serve a gateway fake that answers a chat stream with
: keepaliveand thendata: {"error": {"message": "x", "type": "Timeout", "code": "408"}}(tests/support/llm_stream_fakes.py:serve_llm,reply,sse_data). - Call
list(LLMClient(AIConfig(provider="gateway", ...)).chat_stream("s", "u")).
Expected vs Actual Behavior
Expected: an AIClientError with status 408, as chat gives (test_an_error_frame_with_a_status_gives_that_status). For a 403 frame with a key-limit body: AIKeySpendLimitError.
Actual: a raw openai.APIError, or a connection error with no status.
Environment Diagnostics
Found on 2026-11-23 on branch fix/6821-provider-key-spend-limit, pydantic-ai 2.71.0, openai 3.49.0.
Logs, Traces, or Terminal Output
openai.APIError: x from openai/_streaming.py:103, outside pytest.raises(AIClientError).
Alternatives check of 2026-11-23: none.
Tool: none found.
Reason: Searches on SSE error frames and HTTP status handling found only protocol documentation, no tool or library; the fix belongs in the project's own gateway client, which must map the frame's code through the existing _status_error path.
Lacks:
- Mapping of an error frame's
codeto a status - Credential error for a 401 or 403 frame
- Spend-limit error for a spent provider key frame
Proposed design
Problem statement
LLMClient.chat_stream through the gateway does not read the status of an SSE error frame. After a keepalive comment the gateway has sent its status line, so LiteLLM writes its error reply into a frame whose code holds the HTTP status. chat maps that frame through _status_error and gives the status, a credentials error for 401 or 403, and the spend-limit error of a spent provider key. chat_stream does not map it, so a raw openai.APIError escapes or the call reports a connection error with no status.
Acceptance criteria
Requirement: Status of a stream error frame
LLMClient.chat_stream SHALL raise an AIClientError that carries the status that an SSE error frame names in its code.
Scenario: A 408 error frame after a keepalive
- WHEN The gateway fake answers a chat stream with
: keepaliveand thendata: {"error": {"message": "x", "type": "Timeout", "code": "408"}}, and a test consumesLLMClient(AIConfig(provider="gateway", ...)).chat_stream("s", "u")withlist(). - THEN The call raises
AIClientErrorwithstatus == 408.
Requirement: Credentials error from a stream error frame
LLMClient.chat_stream SHALL give the credentials error that chat gives when an SSE error frame names 401 or 403.
Scenario: A 403 error frame with a plain body
- WHEN The gateway fake answers a chat stream with
: keepaliveand thendata: {"error": {"message": "x", "type": "invalid_request_error", "code": "403"}}, and a test consumes the stream withlist(). - THEN The call raises
AIClientErrorwithstatus == 403, and its message equals the message thatclient._status_error(403, body)gives for the same error body.
Requirement: Spend-limit error from a stream error frame
LLMClient.chat_stream SHALL raise AIKeySpendLimitError when a 403 SSE error frame carries the body of a spent provider key.
Scenario: A 403 error frame with a key-limit body
- WHEN The gateway fake answers a chat stream with
: keepaliveand then an error frame withcode"403"and the key-limit body thatspend_limit_errorrecognizes, and a test consumes the stream withlist(). - THEN The call raises
AIKeySpendLimitError.
Requirement: A stream error frame with no status
LLMClient.chat_stream SHALL raise AIClientError and SHALL NOT let an openai.APIError escape when an SSE error frame names no numeric status.
Scenario: An error frame whose code is not a number
- WHEN The gateway fake answers a chat stream with
: keepaliveand thendata: {"error": {"message": "x", "type": "invalid_request_error", "code": "invalid_request_error"}}, and a test consumes the stream withlist(). - THEN The call raises
AIClientErrorand noopenai.APIErrorescapes.
Existing Tool Considered
The alternatives check of 2026-11-23 in the issue body gives the verdict none and names no tool: no open, maintained tool or library covers the need. The fix belongs in the project's own gateway client. The change supplies the three lacks that the block lists: it maps the frame's code through _frame_status, and _status_error then gives the status, the credentials error for a 401 or 403 frame, and the spend-limit error for a spent key. Cost of the own code: one catch clause and about 5 changed lines in src/devops_cli/ai/client/openai.py, an estimated 400 tokens to write, near zero tokens to keep, and under one hour of elapsed time.
Key questions
- Why does an error frame escape
chat_stream?:_openai_compat_streamcatches onlyopenai.APIStatusErrorandopenai.APIConnectionError. Anopenai.APIErrorraised while_consume_streamreads the frames is not mapped._map_openai_erroralready maps it: it reads_frame_status(exc)and calls_status_error. [sources: src/devops_cli/ai/client/openai.py, src/devops_cli/ai/client/openai.py:524] - Where should the stream path map an error frame?: Catch
openai.APIErrorin_openai_compat_streamand raiseself._map_openai_error(exc). One mapping path then serveschatandchat_stream._consume_streamstays free of provider error mapping. (best practice) [sources: src/devops_cli/ai/client/openai.py, src/devops_cli/ai/client/openai.py:126]- Chosen: Catch
openai.APIErrorin_openai_compat_stream. It reuses_map_openai_error, the one path that already reads the frame'scode, and it changes one clause. - Rejected: Map the frame inside
_consume_stream. It would put provider error mapping in the frame reader, which also serves replies that are not gateway errors. - Rejected: Map the frame in the
chat_streamwrapper of the unified client. It would leave the provider backend able to raise a rawopenai.APIError, and it adds a second mapping path.
- Chosen: Catch
- Which error does a 401 or 403 error frame give?:
_status_errortriesspend_limit_error(status, raw_body)first for 401 and 403. A key-limit body givesAIKeySpendLimitError. Any other 401 or 403 givescredentials_error. [sources: src/devops_cli/ai/client/openai.py:126, src/devops_cli/ai/client/models.py:150] - Which test file holds the new stream cases?: The new cases belong in
tests/ai/client/test_ai_streaming.py, beside the existing stream tests. They use theserve_llm,replyandsse_datahelpers oftests/support/llm_stream_fakes.py. [sources: tests/ai/client/test_ai_streaming.py, tests/support/llm_stream_fakes.py]
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Activity
dan-petty commentedon Nov 23, 2026
Both items deal with a lost HTTP status in AI client error handling, but 3797 fixes Claude/Anthropic losing status after retries while this fixes chat_stream not reading status from an SSE error frame.devops-cli-service commentedon Nov 23, 2026
The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.
Searches (2)
Searches run:
Sources:
devops-cli-service commentedon Nov 23, 2026
Next step: the stage job writes the plan of the item. See the section Proposed design of the body.
devops-cli-service commentedon Nov 25, 2026
Released in v0.218.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.218.0
devops-cli-service commentedon Nov 25, 2026
Live since 2026-11-25 07:20 UTC