Skip to content

feat(nah_chat): add Responses API support - #27

Merged
linmx0130 merged 11 commits into
mainfrom
feat/responses-api
Aug 2, 2026
Merged

linmx0130 merged 11 commits into
mainfrom
feat/responses-api

Conversation

@linmx0130

Copy link
Copy Markdown
Owner

Adds OpenAI Responses API support to nah_chat (stream + non-stream), tested live against DeepSeek deepseek-v4-flash (https://api.deepseek.com/responses).

What's included

  • nah_chat/src/responses.rs (new): ResponsesInput/ResponseInputItem, ResponseObject (+ output_text()/reasoning_text()/function_calls()), ResponseUsage, ResponsesStreamEvent (11 variants), ResponsesParamsBuilder, buffered SSE parser, impl ChatClient { create_responses_request, responses, responses_stream }
  • 15 new network-free unit tests (SSE event dispatch, chunk-split reassembly incl. CRLF boundary split, event-line fallback, [DONE] tolerance, input serialization, request body shape)
  • Examples: examples/responses.rs (text + reasoning, stream & non-stream), examples/responses_tool.rs (function calling, flat tool schema)
  • Bug fix: chat_completion_stream infinite loop when the server closes the connection without data: [DONE]
  • Version bump 0.7.0, README/CHANGELOG/docs

Live verification (DeepSeek V4 Flash)

  • Non-stream: full text + usage (reasoning_tokens reported)
  • Stream: reasoning_text.delta (chain-of-thought) + output_text.delta, terminates on response.completed
  • Tool call: function_call_arguments.delta streamed {"city": "San Francisco"}; Completed output item carries name=get_weather, arguments={...}

Notes

  • DeepSeek Responses API supports only deepseek-v4-flash for now (v4-pro in early Aug 2026); unsupported params are silently ignored server-side
  • Responses-API tools use the FLAT schema ({"type":"function","name":...,"parameters":...}), unlike chat completions
  • nah crate integration (migrating nah/src/chat.rs to the Responses API) is a follow-up

@linmx0130
linmx0130 merged commit 02b9d06 into main Aug 2, 2026
1 check passed
@linmx0130
linmx0130 deleted the feat/responses-api branch August 2, 2026 04:27
linmx0130 added a commit that referenced this pull request Aug 2, 2026
…error surfacing (#28)

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
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