Skip to content

feat(llm): SSE streaming fallback for OpenAI & Anthropic chat#35

Merged
tang-vu merged 5 commits into
tang-vu:mainfrom
Nam0101:feat/llm-sse-fallback
May 23, 2026
Merged

feat(llm): SSE streaming fallback for OpenAI & Anthropic chat#35
tang-vu merged 5 commits into
tang-vu:mainfrom
Nam0101:feat/llm-sse-fallback

Conversation

@Nam0101

@Nam0101 Nam0101 commented May 16, 2026

Copy link
Copy Markdown
Contributor

Description

Type

  • feat – New feature
  • fix – Bug fix
  • refactor – Code restructuring
  • docs – Documentation
  • test – Tests
  • perf – Performance
  • chore – Maintenance

Changes

Architecture Area

  • Middleware chain (core/middleware.py)
  • Analysis / Skills (analysis/analyzer.py, analysis/skills.py)
  • Sub-agents (agents/registry.py)
  • Tools (tools/protocol.py)
  • Memory / Learning (orchestrator/memory.py)
  • PR Patrol (pr/patrol.py)
  • Pipeline (orchestrator/pipeline.py)
  • Other:

Pre-submit Checklist

⚠️ CI will auto-run on your PR. Please verify locally before pushing.

  • ruff check contribai/ passes (lint)
  • ruff format --check contribai/ tests/ passes (format)
  • pytest tests/ -v passes (356+ tests)
  • Code follows project conventions (async, type hints, Google docstrings)
  • from __future__ import annotations at top of new files
  • No hardcoded secrets
  • DCO signoff on all commits (git commit -s)

Related Issues

NamNV added 2 commits May 17, 2026 02:11
When the non-streaming POST returns a 5xx or fails with a transport
timeout/connect error, retry the same request with `stream: true` and
parse the SSE stream into the same String the trait already returns.
Non-retryable errors (4xx including 429) bubble up unchanged.

Adds shared `drain_sse_events` helper plus provider-specific parsers
for OpenAI Chat Completions (`choices[0].delta.content`, stops on
`[DONE]`) and Anthropic Messages (`content_block_delta.delta.text`,
stops on `message_stop`, surfaces `error` events).

Enables the `stream` feature on reqwest.
Wiremock-driven integration tests covering both branches added in the
previous commit:
  - 5xx on the non-stream POST → retry with stream:true and parse SSE
  - 4xx (400 / 401) → bubble up unchanged, no fallback

Each test asserts the parsed text matches the streamed deltas and that
the mock observed exactly one non-stream and zero or one stream call,
so a regression that skips or duplicates the fallback path is caught.

Adds a sanity factory check that create_llm_provider_raw still wires
OpenAIProvider and AnthropicProvider.
@Nam0101 Nam0101 requested a review from tang-vu as a code owner May 16, 2026 19:17

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d9ddfbf969

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/contribai-rs/src/llm/provider.rs Outdated
NamNV added 2 commits May 17, 2026 02:22
reqwest::bytes_stream() can split a multi-byte UTF-8 codepoint across
chunks. The previous parser called from_utf8 on each raw chunk and
silently dropped any chunk that ended mid-codepoint, which truncated
streamed completions containing accents, emoji, or CJK text.

Buffer the stream as Vec<u8> and only decode once a complete \n\n
event has been collected, so partial codepoints stay in the buffer
until their continuation bytes arrive.

Adds a regression test that splits the 4-byte 🌏 mid-codepoint and a
wiremock test streaming Vietnamese + emoji + CJK end-to-end through
OpenAIProvider::chat.
Some OpenAI-compatible proxies (e.g. nip.io routers fronting Claude)
ignore stream:false and always reply with Content-Type: text/event-stream
+ HTTP 200. The previous code only checked status, then ran .json() on
the response and failed every call with "JSON parse error".

Inspect Content-Type on success: when it is text/event-stream, route
the response through the existing SSE parser instead of decoding JSON.

Adds two regression tests using set_body_raw so the mock advertises the
SSE content type.
@Nam0101 Nam0101 force-pushed the feat/llm-sse-fallback branch from 0bd7826 to e037f91 Compare May 16, 2026 20:59
@tang-vu tang-vu merged commit d3e5481 into tang-vu:main May 23, 2026
5 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.

2 participants