Skip to content

feat(server): align Anthropic Messages generation and stop semantics - #3696

Merged
inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:feat/anthropic-messages-3625
Dec 2, 2026
Merged

inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:feat/anthropic-messages-3625

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

Align Anthropic Messages generation and stop semantics

Fixes #3625.

Claude Code can probe the server with GET or HEAD /api/hello. The probe returns an empty 200 and follows the normal API-key policy. Messages requests translate thinking.type and output_config.effort into the existing template controls; explicit template kwargs still take precedence.

Both JSON and SSE responses honor disable_parallel_tool_use. Text-only requests clamp the generation budget to the space left in the slot, unless context shifting is enabled. A length finish takes precedence over tool use or a stop sequence; only a response that actually reports stop_sequence includes the matched string. The OpenAI routes are unchanged.

Verification

  • CPU build: all 123 Anthropic library tests passed, including both response modes, the protected probe and the context clamp.
  • CPU checkpoint smoke: mlx-community/Qwen3-0.6B-4bit returned only a text block with thinking disabled. GET /api/hello returned HTTP 200 with Content-Length: 0.
  • cargo fmt --check and git diff --check passed.
  • The default Metal test build, make verify-test and workspace Clippy were not verified. This host lacks the Xcode Metal compiler; the workspace test and Clippy builds stopped during native shader compilation.

On DGX Spark 2, the pre-review patch passed 122 focused Anthropic tests and workspace Clippy. A live CUDA Qwen3.5-4B-4bit server returned 200 for GET and HEAD /api/hello and emitted a thinking block only when enabled. The full test run failed on five targets; the clean base reproduced the same 70 named failures and two process aborts. The aborted binaries did not finish. These Spark runs preceded the final stop-sequence correction; the 123-test CPU run above includes it.

@inureyes inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:medium Medium priority labels Dec 2, 2026
A generation the scheduler stops at the per-slot context bound (StopKind::ContextExhausted, finish_reason "length") now reports stop_reason model_context_window_exceeded on /v1/messages, streaming and non-streaming, not only when the route clamped max_tokens. Requests with media are never clamped, so this was the only way they could report it.

The Messages response now always serializes stop_sequence, as null when nothing matched, so the non-streaming body and message_start match the API shape and the closing message_delta.

Adds route tests through the scripted provider for a scheduler-matched stop sequence in both modes (reason, matched string, no leaked text), the null field, the context-exhausted stop, and the end-to-end max_tokens clamp including the context-shift exemption. The scripted provider gains a finish step with an explicit finish reason and stop kind. Documents /api/hello in the server feature table.

Refs lablup#3625, lablup#3696
@inureyes

inureyes commented Dec 2, 2026

Copy link
Copy Markdown
Member

Hardening review

Reviewed against #3625 (items 1-6) and the Messages API stop semantics. Items 1-6 are implemented and wired into both the streaming and non-streaming /v1/messages paths. Straddling stop sequences are held back by the scheduler (#1466), so streamed text never includes a partial match.

Fixes pushed in bf0fc4e:

  • A sequence the scheduler stops at the per-slot context bound (StopKind::ContextExhausted, finish length) now reports model_context_window_exceeded, not max_tokens. Previously only the route clamp set it, so media requests (never clamped) or an underestimated prompt count reported max_tokens.
  • stop_sequence is always serialized (null when nothing matched) on the non-streaming body and message_start, matching the API shape and the closing message_delta.
  • Route tests through the scripted provider: a scheduler-matched stop sequence in both modes (reason, matched string, no leaked text), the explicit null, the context-exhausted stop, and the end-to-end clamp (dispatched max_tokens, reported reason, --context-shift exemption). The scripted provider gained a finish step with an explicit finish reason and stop kind.
  • /api/hello added to the HTTP surfaces table in docs/server-features.md.

Security: /api/hello stays behind the API-key middleware (covered by the auth test). Performance: the non-streaming path now tokenizes the prompt once more for the clamp, as the streaming path already did.

Merged current main (9ecd6df). Validation on Linux (CPU build): cargo fmt --check, cargo clippy -p mlxcel --lib --tests -D warnings, and 235 lib tests matching anthropic, tool_call_stream, auth_route and model_provider all pass.

@inureyes
inureyes merged commit bae0c18 into lablup:main Dec 2, 2026
36 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Anthropic Messages API parity with mlx-lm server_v2

2 participants