Skip to content

fix(anthropic_messages): gate sampling params on /v1/messages like /chat/completions - #35057

Closed
mihidumh wants to merge 1 commit into
BerriAI:litellm_internal_stagingfrom
mihidumh:fix/anthropic-messages-sampling-params
Closed

fix(anthropic_messages): gate sampling params on /v1/messages like /chat/completions#35057
mihidumh wants to merge 1 commit into
BerriAI:litellm_internal_stagingfrom
mihidumh:fix/anthropic-messages-sampling-params

Conversation

@mihidumh

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • /v1/messages forwards temperature/top_p/top_k raw to models that removed them
  • Provider 400s; router fallbacks mask them as silent model downgrades
  • /chat/completions already drops the same params for the same models

How it solves it:

  • Reuse the chat path's AnthropicModelInfo._apply_sampling_param gating in the /v1/messages param builder
  • Drop under drop_params (matching /chat/completions), else clean client-side 400
  • No-op for models that still support sampling params (temperature=1 stays allowed everywhere)

Relevant issues

Fixes #35053

Linear ticket

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • 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?

Screenshots / Proof of Fix

Real Vertex AI call (project-billed, no mocks), identical script before/after this change — litellm.anthropic_interface.acreate with model="vertex_ai/claude-sonnet-5", temperature=0.3, top_p=0.9, top_k=40, litellm.drop_params = True:

Before (base branch) — provider rejects the forwarded param:

litellm.llms.base_llm.chat.transformation.BaseLLMException: {"type":"error","error":
{"type":"invalid_request_error","message":"`temperature` is deprecated for this model."},
"request_id":"req_vrtx_011CdVrHWmLgPLyK75cwfNSC"}

After (this branch) — params dropped, request succeeds:

model: claude-sonnet-5 | stop_reason: end_turn
content: [{'type': 'text', 'text': 'Ok'}]

The same asymmetry was reproduced through a full proxy deployment (litellm 1.94.0 on the stock docker image) in #35053, including the silent-downgrade behavior when router_settings.fallbacks are configured — full 16-param × 2-endpoint × 2-generation sweep table there.

Unit tests: tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_request_optional_param_utils.py — drop for supports_sampling_params: false models (bare and vertex_ai/-prefixed ids), keep for supporting models, temperature=1 allowed, clean UnsupportedParamsError without drop_params. tests/test_litellm/llms/anthropic/ locally: 1184 passed vs 1179 on base, same 41 pre-existing env-dependent failures in both runs.

@greptile-apps

greptile-apps Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

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

  • Applies existing model-capability gating to temperature, top_p, and top_k.
  • Drops unsupported parameters when configured and otherwise raises a client-side unsupported-parameter error.
  • Adds coverage for unsupported, provider-prefixed, supporting, and temperature=1 cases.

Confidence Score: 5/5

The PR appears safe to merge, with the new messages-path behavior matching the established chat-path sampling rules.

The changed path delegates capability decisions and global or per-request drop behavior to the existing Anthropic helper, while preserving supported parameters and the allowed temperature value.

Important Files Changed

Filename Overview
litellm/llms/anthropic/experimental_pass_through/messages/utils.py Reuses the existing Anthropic sampling-capability helper to filter or reject unsupported parameters without introducing a concrete regression.
tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_request_optional_param_utils.py Adds focused unit coverage for dropping, preserving, and rejecting sampling parameters across relevant model forms.

Reviews (1): Last reviewed commit: "fix(anthropic_messages): gate sampling p..." | 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-hq

codspeed-hq Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing mihidumh:fix/anthropic-messages-sampling-params (9653f58) with litellm_internal_staging (47a7e17)1

Open in CodSpeed

Footnotes

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

@mihidumh
mihidumh force-pushed the fix/anthropic-messages-sampling-params branch from 8ef7709 to 06e4dcb Compare August 20, 2026 05:50
…hat/completions

/v1/messages forwarded temperature/top_p/top_k raw to models that removed
sampling params (supports_sampling_params: false — Claude 4.7+/Fable 5),
producing provider 400s that router fallbacks mask as silent model
downgrades. The chat path already gates these via
AnthropicModelInfo._apply_sampling_param; reuse it in
get_requested_anthropic_messages_optional_param so both endpoints agree:
drop under drop_params, else raise the clean client-side 400.

Fixes BerriAI#35053
@mihidumh

Copy link
Copy Markdown
Contributor Author

Closing — superseded by #37868, which merged this branch's commit (authorship preserved) into litellm_internal_staging on 2026-08-22. Thanks @tin-berri.

@mihidumh mihidumh closed this Aug 23, 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

1 participant