Skip to content

refactor(gateway): extract shared streaming generator utility#930

Merged
njbrake merged 2 commits intomainfrom
refactor/shared-streaming
Mar 13, 2026
Merged

refactor(gateway): extract shared streaming generator utility#930
njbrake merged 2 commits intomainfrom
refactor/shared-streaming

Conversation

@njbrake
Copy link
Copy Markdown
Contributor

@njbrake njbrake commented Mar 13, 2026

Description

Extracts the duplicated SSE streaming logic from routes/chat.py and routes/messages.py into a shared streaming_generator() in gateway/streaming.py.

Both the OpenAI chat completions and Anthropic messages endpoints had structurally identical streaming generators (~50-60 lines each) handling: chunk iteration, usage accumulation, done markers, error SSE events, and usage logging. The only differences were protocol-specific formatting details.

The new shared generator accepts protocol-specific callbacks (format_chunk, extract_usage, on_complete, on_error) and a StreamFormat dataclass that captures the done marker, error payload, and whether to yield a done marker after errors. Pre-built OPENAI_STREAM_FORMAT and ANTHROPIC_STREAM_FORMAT constants are provided.

PR Type

  • 💅 Refactor

Relevant issues

Fixes #919

Checklist

  • I understand the code I am submitting.
  • I have added unit tests that prove my fix/feature works
  • I have run this code locally and verified it fixes the issue.
  • New and existing tests pass locally
  • Documentation was updated where necessary
  • I have read and followed the contribution guidelines
  • AI Usage:
    • No AI was used.
    • AI was used for drafting/refactoring.
    • This is fully AI-generated.

AI Usage Information

  • AI Model used: Claude Opus 4.6

  • AI Developer Tool used: Claude Code

  • Any other info you'd like to share: Identified and implemented as part of a gateway codebase improvement sweep.

  • I am an AI Agent filling out this form (check box if true)

Extract duplicated SSE streaming logic from chat.py and messages.py into
a shared streaming_generator() in gateway/streaming.py. Both endpoints
now use the same generator with protocol-specific format/extract/callback
functions, ensuring consistent error handling and usage tracking.

Fixes #919

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@njbrake njbrake temporarily deployed to integration-tests March 13, 2026 14:40 — with GitHub Actions Inactive
@njbrake njbrake had a problem deploying to integration-tests March 13, 2026 14:40 — with GitHub Actions Failure
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
see 42 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Add return type annotations to async generator helpers in
  test_streaming_generator.py (mypy no-untyped-def)
- Extract exception message strings to module-level constants
  (ruff EM101/TRY003)
- Update test_streaming_error_event.py to expect HTTP 500 for stream
  creation failures, matching the refactored behavior where
  acompletion() is called before the streaming generator starts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@njbrake njbrake temporarily deployed to integration-tests March 13, 2026 14:45 — with GitHub Actions Inactive
@njbrake njbrake temporarily deployed to integration-tests March 13, 2026 14:45 — with GitHub Actions Inactive
@njbrake njbrake merged commit 443545e into main Mar 13, 2026
12 checks passed
@njbrake njbrake deleted the refactor/shared-streaming branch March 13, 2026 14:49
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.

refactor(gateway): extract shared streaming generator logic from chat and messages routes

1 participant