Skip to content

fix(nah_chat): chat completion stream event loss, reasoning_content, error surfacing - #28

Merged
linmx0130 merged 1 commit into
mainfrom
fix/chat-completion
Aug 2, 2026
Merged

linmx0130 merged 1 commit into
mainfrom
fix/chat-completion

Conversation

@linmx0130

Copy link
Copy Markdown
Owner

Fixes for the chat completion API found by live-testing against DeepSeek V4 Flash. Stacked on #27 (based on feat/responses-api to reuse its buffered SSE parser); the diff will shrink to just these fixes once #27 merges.

Problems found & fixed

P0 — stream event loss (up to 61% of deltas)

chat_completion_stream split each raw res.chunk() on \n\n with no buffering — any SSE event crossing a network-chunk boundary was silently dropped. Replaying a real captured DeepSeek stream through the old parser at typical chunk sizes lost 22/36 deltas (511B) and 2/36 (4KB). This corrupts streaming text and tool-call argument deltas.

Fixed by reusing responses::SseBuffer (buffered, CRLF-safe). After the fix: 0 loss at every chunk size from 3B to 4KB on the same real stream. Regression test: test_chat_stream_chunk_split_reassembly.

P1 — non-stream drops reasoning_content

ChatMessage.reasoning_content was serde-renamed to reasoningContent (camelCase) while DeepSeek/Qwen send reasoning_content — chain-of-thought silently vanished from non-stream responses (stream mode already used the snake_case name and worked). Renamed to reasoning_content; verified live (non-stream now returns the reasoning text). Tests: test_deserialize_reasoning_content_snake_case, test_serialize_reasoning_content_snake_case.

P1 — misleading error on 4xx/5xx (non-stream)

chat_completion had no HTTP status check, so a 400 (e.g. unknown model) surfaced as "Invalid response format: missing choices field" instead of the server's message. Now mirrors the stream path and returns the server error body. Verified live: invalid model → real 400 body with the supported-model list.

Live verification (deepseek-v4-flash)

  • non-stream + thinking: content + reasoning_content present
  • stream + thinking: text deltas + reasoning deltas, clean termination
  • tool call (nested schema) + tool result round-trip: complete arguments
  • error paths: proper 400/401 bodies

…error surfacing

- chat_completion_stream now buffers SSE via responses::SseBuffer; events
  split across network chunks were silently dropped (empirically up to 22/36
  deltas lost with 511-byte chunks on a real DeepSeek stream). After the fix:
  0 loss at every chunk size from 3B to 4KB.
- ChatMessage.reasoning_content now (de)serializes as reasoning_content
  (was reasoningContent) so non-stream chat_completion no longer drops
  chain-of-thought from DeepSeek/Qwen-style providers.
- chat_completion (non-stream) now checks HTTP status and surfaces the
  server error body instead of a misleading 'missing choices field' error.
- Regression tests: chunk-split reassembly, reasoning_content round-trip.
@linmx0130
linmx0130 force-pushed the fix/chat-completion branch from cdb376f to 35c2c85 Compare August 2, 2026 04:28
@linmx0130
linmx0130 merged commit 15880fa into main Aug 2, 2026
1 check passed
@linmx0130
linmx0130 deleted the fix/chat-completion branch August 2, 2026 04:29
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.

1 participant