Skip to content

feat(anthropic): /v1/messages SSE streaming + tool_use/tool_result blocks - #5

Merged
hanzo-dev merged 1 commit into
mainfrom
feat/anthropic-streaming
May 30, 2026
Merged

hanzo-dev merged 1 commit into
mainfrom
feat/anthropic-streaming

Conversation

@hanzo-dev

Copy link
Copy Markdown
Member

Summary

  • Anthropic SSE event sequence on /v1/messages: message_start, content_block_start, content_block_delta/text_delta, content_block_stop, message_delta, message_stop
  • ToolUse / ToolResult variants added to AnthropicContentBlock
  • anthropic_tools_to_openai maps Anthropic {name, description, input_schema} -> OpenAI function-tool shape
  • translate_message expands mixed Anthropic content (text + tool_use + tool_result) into OpenAI messages: assistant with tool_calls, separate role:tool messages with tool_call_id
  • Non-streaming path also surfaces tool_use blocks via build_content_blocks when choices[0].message.tool_calls is non-empty
  • Tool-call streaming emits input_json_delta with prefix-stripping in handle_tool_calls

Tests

7/7 unit tests in mistralrs-server-core::anthropic pass (translation, streaming sequence, tool_use, event framing). End-to-end SSE verified against Qwen3-0.6B on a temporary :36901 server — full event sequence observed.

Gaps (deferred)

  • Input image content blocks still dropped (pre-existing; out of scope)
  • message_id uses msg_<seq> rather than Anthropic-style UUID

Builds on the non-streaming Anthropic path landed earlier in 76af347.

Expands the Anthropic /v1/messages handler from a non-streaming text-only stub
into a full Claude Code-compatible endpoint:

- Streaming: when stream=true, returns an SSE response that emits the documented
  Anthropic event sequence (message_start, ping, content_block_start,
  content_block_delta(text_delta|input_json_delta), content_block_stop,
  message_delta, message_stop). Builds on the existing chat-pipeline response
  channel, translating each ChatCompletionChunkResponse / Done into Anthropic
  events via a state machine that tracks open text and tool_use blocks.
- Tool_use blocks: AnthropicContentBlock now has ToolUse and ToolResult
  variants. The request translator splits each Anthropic message into the
  appropriate OpenAI messages (assistant w/ tool_calls, separate `tool` role
  messages with tool_call_id). The response builder emits tool_use blocks
  whenever the OpenAI message has tool_calls (streaming and non-streaming).
- Anthropic tools schema (name/description/input_schema) is translated to the
  OpenAI function-tool shape so existing tool plumbing applies as-is.

7 colocated unit tests cover the translation, content block construction, and
the streaming event sequence (text-only and tool_use). End-to-end SSE was
verified against a live Qwen3-0.6B server on :36901: full
message_start...message_stop sequence emitted correctly.
@github-actions

Copy link
Copy Markdown
Code Metrics Report
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 Language              Files        Lines         Code     Comments       Blanks
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 C Header                  5          305          210           52           43
 CSS                       5         1462         1288           39          135
 CUDA                     60        20622        16570         1656         2396
 Dockerfile                1           38           21            8            9
 JavaScript               19         3972         3089          488          395
 Jinja2                    7          694          656            5           33
 JSON                     27         9400         9397            0            3
 Makefile                  1            6            5            0            1
 MDX                       1          147            0          132           15
 Metal Shading Lan|       34        13466        10542         1117         1807
 PowerShell                1          300          227           30           43
 Python                  131        10161         8357          462         1342
 Shell                     4          948          765          106           77
 Plain Text                3         3723            0         2413         1310
 TOML                     30         1427         1237           46          144
 TypeScript               11         1607         1371           66          170
 YAML                      4           41           39            2            0
─────────────────────────────────────────────────────────────────────────────────
 HTML                      4          782          695           38           49
 |- CSS                    1          578          544           14           20
 |- JavaScript             1           24           23            0            1
 (Total)                             1384         1262           52           70
─────────────────────────────────────────────────────────────────────────────────
 Jupyter Notebooks         3          122           83           23           16
 |- Markdown               1           60           30           22            8
 |- Python                 1          122          113            1            8
 (Total)                              304          226           46           32
─────────────────────────────────────────────────────────────────────────────────
 Markdown                210        19178            0        13456         5722
 |- BASH                 123         1399         1090          190          119
 |- Dockerfile             3            6            6            0            0
 |- JSON                  35         1175         1175            0            0
 |- PowerShell             5            7            7            0            0
 |- Python                45         1717         1459           65          193
 |- Rust                  64         2423         2048           56          319
 |- TOML                  16          318          250            4           64
 |- YAML                   3           18           17            1            0
 (Total)                            26241         6052        13772         6417
─────────────────────────────────────────────────────────────────────────────────
 Rust                    602       259959       229293         6654        24012
 |- Markdown             400         9599          454         7977         1168
 (Total)                           269558       229747        14631        25180
─────────────────────────────────────────────────────────────────────────────────
 Svelte                   18         1831         1696           50           85
 |- CSS                    1            4            4            0            0
 |- JavaScript            18          876          727           24          125
 (Total)                             2711         2427           74          210
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 Total                  1181       368517       293488        35197        39832
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

@hanzo-dev
hanzo-dev merged commit 052099a into main May 30, 2026
1 of 2 checks passed
hanzo-dev added a commit that referenced this pull request Jul 10, 2026
…fall #5)

Hoist Tensor::arange (timestep freqs) and Tensor::from_vec (causal mask) out
of the per-forward hot loop to model init, eliminating a CPU-GPU sync per DiT
forward (~310 per 10-step rollout). Parity preserved bit-exact (DiT cosine
1.000000). Also: example now reports per-stage timing (vae-enc/dit/vae-dec).
This is also the precondition for capturing the fixed-shape denoise step as a
CUDA graph.
hanzo-dev added a commit that referenced this pull request Jul 16, 2026
…fall #5)

Hoist Tensor::arange (timestep freqs) and Tensor::from_vec (causal mask) out
of the per-forward hot loop to model init, eliminating a CPU-GPU sync per DiT
forward (~310 per 10-step rollout). Parity preserved bit-exact (DiT cosine
1.000000). Also: example now reports per-stage timing (vae-enc/dit/vae-dec).
This is also the precondition for capturing the fixed-shape denoise step as a
CUDA graph.
zeekay pushed a commit that referenced this pull request Jul 26, 2026
…fall #5)

Hoist Tensor::arange (timestep freqs) and Tensor::from_vec (causal mask) out
of the per-forward hot loop to model init, eliminating a CPU-GPU sync per DiT
forward (~310 per 10-step rollout). Parity preserved bit-exact (DiT cosine
1.000000). Also: example now reports per-stage timing (vae-enc/dit/vae-dec).
This is also the precondition for capturing the fixed-shape denoise step as a
CUDA graph.
hanzo-dev added a commit that referenced this pull request Jul 27, 2026
…fall #5)

Hoist Tensor::arange (timestep freqs) and Tensor::from_vec (causal mask) out
of the per-forward hot loop to model init, eliminating a CPU-GPU sync per DiT
forward (~310 per 10-step rollout). Parity preserved bit-exact (DiT cosine
1.000000). Also: example now reports per-stage timing (vae-enc/dit/vae-dec).
This is also the precondition for capturing the fixed-shape denoise step as a
CUDA graph.
zeekay added a commit that referenced this pull request Sep 15, 2026
…fall #5)

Hoist Tensor::arange (timestep freqs) and Tensor::from_vec (causal mask) out
of the per-forward hot loop to model init, eliminating a CPU-GPU sync per DiT
forward (~310 per 10-step rollout). Parity preserved bit-exact (DiT cosine
1.000000). Also: example now reports per-stage timing (vae-enc/dit/vae-dec).
This is also the precondition for capturing the fixed-shape denoise step as a
CUDA graph.
zeekay added a commit that referenced this pull request Sep 15, 2026
…fall #5)

Hoist Tensor::arange (timestep freqs) and Tensor::from_vec (causal mask) out
of the per-forward hot loop to model init, eliminating a CPU-GPU sync per DiT
forward (~310 per 10-step rollout). Parity preserved bit-exact (DiT cosine
1.000000). Also: example now reports per-stage timing (vae-enc/dit/vae-dec).
This is also the precondition for capturing the fixed-shape denoise step as a
CUDA graph.
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