Skip to content

fix: enrich streaming HTTP errors with provider-specific messages#196

Merged
Kamilbenkirane merged 1 commit intomainfrom
fix/streaming-error-enrichment
Feb 26, 2026
Merged

fix: enrich streaming HTTP errors with provider-specific messages#196
Kamilbenkirane merged 1 commit intomainfrom
fix/streaming-error-enrichment

Conversation

@Kamilbenkirane
Copy link
Member

@Kamilbenkirane Kamilbenkirane commented Feb 26, 2026

Problem

Streaming and non-streaming HTTP errors produce inconsistent error messages:

# Non-streaming 401
httpx.HTTPStatusError: Anthropic API error: Invalid API Key

# Streaming 401
httpx.HTTPStatusError: Client error '401 Unauthorized' for url 'https://api.anthropic.com/v1/messages'

After this PR

Both paths produce the same enriched message:

# Non-streaming 401
httpx.HTTPStatusError: Anthropic API error: Invalid API Key

# Streaming 401
httpx.HTTPStatusError: Anthropic API error: Invalid API Key

Changes

  • http.py: Read response body before raise_for_status() in stream_post() and stream_post_ndjson() so JSON error details are available downstream
  • streaming.py: Add enrich_stream_errors() wrapper that catches HTTPStatusError and delegates to the provider's error handler
  • client.py: Wire wrapper into ModalityClient._stream() — covers all providers automatically

Closes #194

Test plan

  • Unit tests for stream_post / stream_post_ndjson HTTP error body readability
  • Unit tests for enrich_stream_errors (enrichment, passthrough, non-JSON body fallback)
  • Full unit test suite passes (502 tests)

Streaming errors now produce the same enriched messages as non-streaming
errors (e.g. "Anthropic API error: Invalid API Key" instead of the
generic "Client error '401 Unauthorized' for url '...'").

Three-layer fix:
- http.py: read response body before raise_for_status() so JSON error
  details are available downstream
- streaming.py: add enrich_stream_errors() wrapper that catches
  HTTPStatusError and delegates to provider error handler
- client.py: wire wrapper into _stream() covering all providers

Closes #194
@claude
Copy link

claude bot commented Feb 26, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@Kamilbenkirane Kamilbenkirane merged commit 7d10721 into main Feb 26, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: streaming HTTP errors lack provider-enriched error messages

1 participant