Skip to content

fix(anthropic): gate sampling params on /v1/messages by supports_sampling_params - #35059

Closed
devin-ai-integration[bot] wants to merge 1 commit into
litellm_internal_stagingfrom
litellm_v1_messages_sampling_params_gating
Closed

devin-ai-integration[bot] wants to merge 1 commit into
litellm_internal_stagingfrom
litellm_v1_messages_sampling_params_gating

Conversation

@devin-ai-integration

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • /v1/messages sent temperature/top_p/top_k to models that reject them
  • Upstream 400 turned into silent fallback model downgrades

How it solves it:

  • /v1/messages now runs the same supports_sampling_params gating as chat

Relevant issues

Fixes #35053

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Live proxy against the real Anthropic API, drop_params: true, model anthropic/claude-opus-4-8

Before, at c274cf3 (parent commit, fix reverted):

$ curl -s -o /tmp/before.json -w "%{http_code}\n" localhost:4000/v1/messages \
  -H 'content-type: application/json' -H 'x-api-key: sk-1234' \
  -d '{"model":"claude-opus-4-8","max_tokens":32,"temperature":0.3,"messages":[{"role":"user","content":"say hi"}]}'
400
$ cat /tmp/before.json
{"error":{"message":"{\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"`temperature` is deprecated for this model.\"},\"request_id\":\"req_011CdVrusRJr4FAWZN5Ddw1Q\"}. Received Model Group=claude-opus-4-8\nAvailable Model Group Fallbacks=None","type":"None","param":"None","code":"400"}}

After, at 371e436:

$ curl -s -o /tmp/after.json -w "%{http_code}\n" localhost:4000/v1/messages \
  -H 'content-type: application/json' -H 'x-api-key: sk-1234' \
  -d '{"model":"claude-opus-4-8","max_tokens":32,"temperature":0.3,"messages":[{"role":"user","content":"say hi"}]}'
200
$ cat /tmp/after.json
{"model":"claude-opus-4-8","id":"msg_011CdVrn9vXA5c3dHb7Kjd6d","type":"message","role":"assistant","content":[{"type":"text","text":"Hi! How can I help you today?"}],"stop_reason":"end_turn","stop_sequence":null,"stop_details":null,"usage":{"input_tokens":9,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":13,"output_tokens_details":{"thinking_tokens":0},"service_tier":"standard","inference_geo":"global"}}

Type

🐛 Bug Fix

Changes

AnthropicMessagesRequestUtils.get_requested_anthropic_messages_optional_param only ran _maybe_drop_speed_param against the model, so temperature, top_p and top_k reached models whose map entry has supports_sampling_params: false (opus-4-7, opus-4-8, opus-5, fable/sonnet-5 generation) and the provider 400'd, while /chat/completions dropped them via AnthropicModelInfo._apply_sampling_param. The nastier symptom is that router fallbacks convert that 400 into a 200 served by an older model generation, so callers silently lose the model they asked for

The same helper now runs on the /v1/messages path:

for param in _SAMPLING_PARAMS:  # temperature, top_p, top_k
    if param not in filtered_params:
        continue
    value = filtered_params.pop(param)
    AnthropicModelInfo._apply_sampling_param(
        optional_params=filtered_params, model=model, param=param,
        value=value, drop_params=drop_params, output_key=param,
    )

so behavior matches chat completions exactly: keep the param when the model supports sampling (or temperature=1), drop it under drop_params, otherwise raise a clean client-side UnsupportedParamsError instead of a provider 400

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/29adb4546877457cbf6432ca2714de8c

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@greptile-apps

greptile-apps Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR aligns Anthropic /v1/messages sampling-parameter handling with chat completions.

  • Gates temperature, top_p, and top_k through the existing model-capability helper.
  • Drops unsupported sampling parameters when configured, otherwise raises UnsupportedParamsError.
  • Adds regression coverage for supported models, unsupported models, drop behavior, and the temperature=1 exception.

Confidence Score: 5/5

The PR appears safe to merge with no actionable defects identified.

The messages path now delegates sampling parameters to the same established capability check used by Anthropic chat handling, and the added tests exercise the significant success and rejection branches.

Important Files Changed

Filename Overview
litellm/llms/anthropic/experimental_pass_through/messages/utils.py Reuses the existing Anthropic sampling-capability logic on the messages path without introducing a concrete behavioral regression.
tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_request_optional_param_utils.py Adds focused regression tests covering all three sampling parameters and the relevant supported, dropped, rejected, and exception paths.

Reviews (1): Last reviewed commit: "fix(anthropic): gate sampling params on ..." | Re-trigger Greptile

@codecov

codecov Bot commented Jul 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_v1_messages_sampling_params_gating (371e436) with litellm_internal_staging (2f7574d)1

Open in CodSpeed

Footnotes

  1. No successful run was found on litellm_internal_staging (c274cf3) during the generation of this report, so 2f7574d was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@yuneng-berri
yuneng-berri deleted the branch litellm_internal_staging September 13, 2026 04:36
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

#35053 was fixed by #37868 on main, so this pull request is closed. Reopen it if something was missed.

@github-actions github-actions Bot closed this Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants