Skip to content

fix(anthropic): reject named tool choices without a name - #17309

Merged
rmccorm4 merged 2 commits into
ai-dynamo:mainfrom
xianlubird:feat/reject-anthropic-tool-choice-without-name
Oct 26, 2026
Merged

rmccorm4 merged 2 commits into
ai-dynamo:mainfrom
xianlubird:feat/reject-anthropic-tool-choice-without-name

Conversation

@xianlubird

@xianlubird xianlubird commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

POST /v1/messages currently turns a malformed named-tool choice into a request to call any available tool. Reject the missing name during Anthropic-to-Chat conversion instead, before backend dispatch.

Anthropic's tool_choice contract requires name when type is tool. The protocol type can deserialize a missing name as the simple Tool variant, so validation must happen before that variant is silently converted to Chat Completions required.

Closes #17307.

Before and after

Consider an agent that advertises two tools but accidentally omits the name of the tool it wants to force:

{
  "model": "my-model",
  "max_tokens": 64,
  "messages": [{"role": "user", "content": "List the current directory."}],
  "tools": [
    {
      "name": "list_directory",
      "input_schema": {
        "type": "object",
        "properties": {"path": {"type": "string"}}
      }
    },
    {
      "name": "get_weather",
      "input_schema": {
        "type": "object",
        "properties": {"city": {"type": "string"}}
      }
    }
  ],
  "tool_choice": {"type": "tool"}
}
Before After
Missing tool name Converted to tool_choice: "required" Conversion fails with tool_choice.name identified
Backend behavior Request reaches the backend, which may select either advertised tool No backend request
HTTP result The regression against unchanged main returned 200 with a scripted backend 400 invalid_request_error, for both unary and streaming requests

The rejected request now returns:

{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "Failed to convert request: tool_choice.name: field required when tool_choice.type is \"tool\""
  }
}

For stream: true, rejection is still an HTTP 400 JSON error before any successful SSE response.

To force the intended tool, the client supplies its name:

"tool_choice": {"type": "tool", "name": "list_directory"}

This valid control continues to return 200 in unary and streaming HTTP replay, and the captured backend request still selects exactly list_directory. The patch does not change the existing auto/any/none conversions.

Validation

  • Red/green HTTP regression: the new missing-name test failed against unchanged main with actual status 200, then passed after the fix.

  • cargo test --locked --no-default-features -p dynamo-llm --test frontend_protocol_validation_http: 20 passed. Covers missing-name rejection without backend dispatch and valid named-tool forwarding in both response modes.

  • cargo test --locked --no-default-features -p dynamo-llm --lib protocols::anthropic: 114 passed.

  • Crate/test clippy with --no-deps -- -D warnings, cargo fmt --check -p dynamo-llm, file-scoped pre-commit, and git diff --check passed. The initial clippy command including workspace dependencies was blocked by the existing macOS-only unused join_reuseport parameter warning in dynamo-runtime; the changed crate and HTTP test are checked with warnings denied.

Validation uses the real HTTP handlers, pinned protocol types, and existing scripted-engine harness on CPU. No GPU/model inference was performed; my-model above is an illustrative deployment name.

Summary by CodeRabbit

  • Bug Fixes
    • Anthropic requests with a named tool choice but no tool name now return a validation error instead of being treated as a required tool choice. This applies to both standard and streaming requests.

Signed-off-by: xianlubird <xianlubird@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@xianlubird
xianlubird temporarily deployed to external_collaborator October 26, 2026 13:42 — with GitHub Actions Inactive
@xianlubird
xianlubird temporarily deployed to external_collaborator October 26, 2026 13:42 — with GitHub Actions Inactive
@github-actions github-actions Bot added external-contribution Pull request is from an external contributor trusted-contributor Org-External user who is trusted to run CI without Org-member approval fix labels Oct 26, 2026
@dynamo-ops

Copy link
Copy Markdown
Contributor

/ok to test 7acb9cd

@github-actions github-actions Bot added the frontend `python -m dynamo.frontend` and `dynamo-run in=http|text|grpc` label Oct 26, 2026
@xianlubird
xianlubird marked this pull request as ready for review October 26, 2026 13:45
@xianlubird
xianlubird requested review from a team as code owners October 26, 2026 13:45
@coderabbitai

coderabbitai Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between ee01209 and 7acb9cd.

Files selected for processing: 2
  • lib/llm/src/protocols/anthropic/types.rs
  • lib/llm/tests/frontend_protocol_validation_http.rs

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

Walkthrough

Anthropic request conversion now rejects a tool_choice of type tool when its name is missing. HTTP tests cover validation before dispatch and successful forwarding of a named tool choice in unary and streaming modes.

Changes

Anthropic tool choice validation

Layer / File(s) Summary
Reject missing tool names
lib/llm/src/protocols/anthropic/types.rs
Tool-choice conversion now returns an error when a tool choice has no name. Anthropic request conversion propagates that error. Other tool-choice mappings remain unchanged.
Verify HTTP validation and dispatch
lib/llm/tests/frontend_protocol_validation_http.rs
Tests verify that unnamed tool choices fail validation before backend dispatch in unary and streaming modes. Tests also verify that a named list_directory choice succeeds and is forwarded as a function choice.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Severity of issue fixed: Low

Merge Risk: Minimal · up to 7acb9

The change is ready to merge after normal checks; no actionable risk remains from this review.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title "fix(anthropic): reject named tool choices without a name" matches the main change: the Anthropic-to-Chat conversion now rejects tool_choice with type "tool" and no name, instead of silently…
Description check Passed The description follows the required template. It has a Summary section with the problem, the fix, and before/after behavior, plus a Validation section with test results. The required Related Issues s…
Linked Issues check Passed Issue #17307 requires conversion to reject tool_choice.type: "tool" without name before backend dispatch. The changed Anthropic converter now propagates the convert_anthropic_tool_choice error. …
Out of Scope Changes check Passed The reviewed changes are limited to Anthropic tool-choice error propagation and its HTTP regression test. The test validates the linked issue behavior and preserves valid named-tool forwarding. No unr…
Full details: Title check

Explanation

The title "fix(anthropic): reject named tool choices without a name" matches the main change: the Anthropic-to-Chat conversion now rejects tool_choice with type "tool" and no name, instead of silently mapping it to Required. It is concise, specific, and follows the conventional-commit style. The title does not mention the new tests, but that detail is not needed.

Full details: Description check

Explanation

The description follows the required template. It has a Summary section with the problem, the fix, and before/after behavior, plus a Validation section with test results. The required Related Issues section is present with "Closes #17307". The template's Overview, Details, and Where should the reviewer start? headings are not used as written, so the structure differs from the template. The issue link satisfies the required reference, and the content covers the template's intent.

Full details: Linked Issues check

Explanation

Issue #17307 requires conversion to reject tool_choice.type: "tool" without name before backend dispatch. The changed Anthropic converter now propagates the convert_anthropic_tool_choice error. The added HTTP regression covers unary and streaming requests, HTTP 400 invalid_request_error, the tool_choice.name field, and zero backend dispatch. The test also covers a valid named-tool control and verifies forwarding of the selected tool. The supplied validation reports 20 HTTP tests and 114 Anthropic protocol tests passed.

Full details: Out of Scope Changes check

Explanation

The reviewed changes are limited to Anthropic tool-choice error propagation and its HTTP regression test. The test validates the linked issue behavior and preserves valid named-tool forwarding. No unrelated implementation or test changes are identified.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@devin-ai-integration

devin-ai-integration Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

✅ Dynamo PR CI passed — run 40365559956 (attempt 1) on c7d0c049b9

Gate checks: ✅ backend-status-check · ✅ deploy-status-check · ✅ dynamo-status-check

Posted automatically by Devin for run 40365559956. Updated on every full-CI run of this PR.

Signed-off-by: Ryan McCormick <rmccormick@nvidia.com>
@rmccorm4
rmccorm4 temporarily deployed to external_collaborator October 26, 2026 17:06 — with GitHub Actions Inactive
@dynamo-ops

Copy link
Copy Markdown
Contributor

/ok to test c7d0c04

@rmccorm4
rmccorm4 enabled auto-merge (squash) October 26, 2026 17:10
@rmccorm4
rmccorm4 merged commit 8a0711b into ai-dynamo:main Oct 26, 2026
118 checks passed
@xianlubird
xianlubird deleted the feat/reject-anthropic-tool-choice-without-name branch October 27, 2026 00:53

This branch was previously deployed

1 inactive deployment
external_collaborator — c7d0c049 Deployed Oct 26, 2026 by rmccorm4 via ok-to-test #38855
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-contribution Pull request is from an external contributor fix frontend `python -m dynamo.frontend` and `dynamo-run in=http|text|grpc` size/M trusted-contributor Org-External user who is trusted to run CI without Org-member approval

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(anthropic): reject a named tool choice without a name

3 participants