← Back to Pull requests

feat(protocols)!: Add token_ids to chat and completion choices #701

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Dec 01, 2026 20:50 UTC
Author

Overview

https://github.com/ai-dynamo/dynamo/issues/19264 part of this

BREAKING CHANGE needed to implement return_token_ids / return_prompt_token_ids through the v2 parser (v1 parser is frozen to be bug-fix only). This feature is implemented in steps and this is step 1. Implemented with some feedback from @ luckyq . thanks!

ai summary to follow

---

Adds vLLM-compatible token-ID fields to the chat and completion choice types in dynamo-protocols, so a server can return generated and prompt token IDs when a request sets vLLM's return_token_ids.

This is a breaking change. ChatChoice, ChatChoiceStream and Choice are not #[non_exhaustive], so the new fields break existing struct literals, and Choice / CreateCompletionResponse become crate-owned rather than re-exported from async-openai. The version is pegged manually at 11.0.0 in protocols/Cargo.toml and the root Cargo.toml, with a matching CHANGELOG.md entry, per release-plz.toml's note that automatic major bumps are blocked and must be pegged by hand. The wire format is unchanged for any client that does not opt in.

This is the first of a sequence: the field has no producer in this repo. Nothing here populates it, and that is deliberate — ai-dynamo/dynamo consumes released crate versions, so the protocol type has to land and ship before the frontend change that fills it can be written. Reviewing this as a pure schema addition is the right frame.

Affected crates: dynamo-protocols (the change), dynamo-parsers and dynamo-demo-server (literal updates only, no behavior change).

Details

vLLM exposes a single request flag, return_token_ids (vllm-project/vllm#22587), and returns both token-ID fields under it. There is no return_prompt_token_ids on the client surface. Placement follows vLLM per endpoint, including its asymmetry:

| Endpoint | Generated IDs | Prompt IDs |
|--------|--------|--------|
| /v1/chat/completions (unary + stream) | choices[].token_ids | response root (a later dynamo change) |
| /v1/completions | choices[].token_ids | choices[].prompt_token_ids |

The asymmetry is intentional rather than an oversight to normalize: Speculators sets only return_token_ids and reads response.choices[0].prompt_token_ids, so moving completion prompt IDs to the root would break it. On streams, token_ids is always the per-chunk delta, never a running total.

All three fields are #[serde(default, skip_serializing_if = "Option::is_none")]. Opt-out omits the key rather than serializing null, matching this crate's existing convention and keeping responses byte-identical for clients that did not ask.

On ownership, per protocols/CLAUDE.md's rubric (item 2 — extending the schema with an inference-serving field): ChatChoice and ChatChoiceStream were already crate-owned, so those are field additions only. The completion Choice was re-exported from async_openai::types::chat, so owning it was necessary to extend it. That forces owning exactly one more type, CreateCompletionResponse, because it holds Vec<Choice>. The cascade stops there — no further types were pulled in. Choice was also removed from the explicit async-openai re-export list in chat.rs to avoid a glob collision with the owned definition in completion.rs.

Both owned types derive utoipa::ToSchema under protocol-schema, as ChatChoice already did, so the generated OpenAPI document shows the new fields rather than stopping at a re-export. That needed two new import slots in src/schema.rs — Logprobs and CompletionFinishReason — because unannotated_dependency_types_are_explicit_import_slots requires every unannotated field type to be a declared slot. CreateCompletionResponse is registered in the schema test's Contract, and protocols/README.md is corrected in three places that still described it as wholly re-exported.

One note on parsers/v1: merge_choice destructures ChatChoiceStream exhaustively, so the new field had to be named. It is bound to _ with a comment. Nothing in v1 populates token IDs, so there is never a value to merge, and carrying them through a jailed region would be a v1 feature change — out of scope here, and consistent with v1 being bug-fix-only. Behavior is unchanged, which the full parser suite confirms.

Bug Fix Evidence

For bug fixes, complete all three subsections below with concrete input and before/after outputs.

1. Minimal Sample Input

Not applicable won't change output by itself

2. Buggy Output (Before)

Not applicable won't change output by itself

3. Expected/Fixed Output (After)

Not applicable won't change output by itself

Validation

cargo fmt -p dynamo-protocols -p dynamo-parsers -p dynamo-demo-server
cargo clippy --locked --no-deps -p dynamo-protocols -p dynamo-parsers -p dynamo-demo-server --all-targets -- -D warnings
cargo check -p dynamo-protocols --all-targets --features protocol-schema
cargo check -p dynamo-parsers -p dynamo-demo-server --all-targets
cargo test -p dynamo-protocols --lib
cargo test -p dynamo-parsers

Where should the reviewer start?

protocols/src/types/completion.rs — the owned Choice and CreateCompletionResponse. This is the substance of the change and where the ownership decision lives.
protocols/src/types/chat.rs — the two field additions and the Choice removal from the re-export list.
parsers/v1/src/tool_calling/jail/mod.rs — the one non-mechanical line, merge_choice's token_ids: _

Diff: 650 lines (+487 / -163) across 8 files Reviewer: luckyq Review duration: 5076m
All Vouch review confidence checks passed
Residual risk 0.43 = Change Risk (0.80) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.80 · 650 lines diff across 8 files
0.80
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
46%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.388
Shallow Review Depth shallow_review_depth
0.344
Sensitive Paths sensitive_paths
0.080
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #701"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: medium confidence: 46.7% reviewed sensitive_module
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 80.2% (HIGH) · Higher = larger blast radius
80.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 46.7% · Lower = rushed or shallow review
46.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 42.7% · Moderate risk
42.7%