Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions litellm/llms/anthropic/experimental_pass_through/messages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,30 @@ def get_requested_anthropic_messages_optional_param(
filtered_params: Final = {k: v for k, v in params.items() if k in valid_keys and v is not None}
if model is not None:
from litellm.llms.anthropic.chat.transformation import AnthropicConfig
from litellm.llms.anthropic.common_utils import AnthropicModelInfo

AnthropicConfig._maybe_drop_speed_param(
model=model,
optional_params=filtered_params,
drop_params=drop_params,
custom_llm_provider=custom_llm_provider,
)
# Claude 4.7+ removed sampling params (the API 400s on top_p, top_k,
# and any temperature other than 1). Mirror the /chat/completions
# gating — drop under drop_params, else raise a clean client-side
# 400 — instead of forwarding a known-rejected param upstream, where
# router fallbacks would mask the provider 400 as a silent model
# downgrade.
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
for param in ("temperature", "top_p", "top_k"):
if param in filtered_params:
AnthropicModelInfo._apply_sampling_param( # pyright: ignore[reportPrivateUsage] # same gating the /chat/completions path applies; forking it would drift
optional_params=filtered_params,
model=model,
param=param,
value=filtered_params.pop(param),
drop_params=drop_params,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Avoid destructive parameter mutation

filtered_params.pop(param) removes each value before the helper conditionally restores it, making parameter ownership and exception behavior harder to reason about; construct the gated result without destructively modifying the dictionary.

Context Used: CLAUDE.md (source)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The chat path does the same pop-then-reapply for top_k in transformation.py. The helper only writes back supported params, so the pop is required.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. _apply_sampling_param takes value as an explicit parameter and only writes it back to optional_params when the param is supported — so the caller must pop before passing. The chat path uses the identical pattern. My concern was unfounded; the code is correct.

Apologies for the noise.

output_key=param,
)
return cast(AnthropicMessagesRequestOptionalParams, filtered_params)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
while resolving the (static) type hints only once per process.
"""

import pytest

import litellm
from litellm.llms.anthropic.experimental_pass_through.messages.utils import (
AnthropicMessagesRequestUtils,
Expand Down Expand Up @@ -88,3 +90,61 @@ def test_drop_params_keeps_speed_for_supporting_model():
litellm.drop_params = original

assert result == {"speed": "fast"}


def test_drop_params_strips_sampling_params_for_unsupported_model(monkeypatch):
# claude-opus-4-7 has supports_sampling_params: false in the model map; the
# API 400s on these rather than ignoring them.
monkeypatch.setattr(litellm, "drop_params", False)
result = AnthropicMessagesRequestUtils.get_requested_anthropic_messages_optional_param(
params={"temperature": 0.3, "top_p": 0.9, "top_k": 40, "stream": True},
model="claude-opus-4-7",
drop_params=True,
)

assert result == {"stream": True}


def test_drop_params_strips_sampling_params_for_provider_prefixed_model(monkeypatch):
# Vertex-routed ids must resolve the same capability flag.
monkeypatch.setattr(litellm, "drop_params", False)
result = AnthropicMessagesRequestUtils.get_requested_anthropic_messages_optional_param(
params={"temperature": 0.3, "top_p": 0.9, "top_k": 40},
model="vertex_ai/claude-opus-4-7",
drop_params=True,
)

assert result == {}


def test_sampling_params_kept_for_supporting_model(monkeypatch):
monkeypatch.setattr(litellm, "drop_params", False)
result = AnthropicMessagesRequestUtils.get_requested_anthropic_messages_optional_param(
params={"temperature": 0.3, "top_p": 0.9, "top_k": 40},
model="claude-sonnet-4-6",
drop_params=True,
)

assert result == {"temperature": 0.3, "top_p": 0.9, "top_k": 40}


def test_temperature_1_kept_for_unsupported_model(monkeypatch):
# temperature=1 is the one value these models still accept.
monkeypatch.setattr(litellm, "drop_params", False)
result = AnthropicMessagesRequestUtils.get_requested_anthropic_messages_optional_param(
params={"temperature": 1},
model="claude-opus-4-7",
drop_params=True,
)

assert result == {"temperature": 1}


def test_sampling_param_raises_clean_400_without_drop_params(monkeypatch):
monkeypatch.setattr(litellm, "drop_params", False)
with pytest.raises(litellm.utils.UnsupportedParamsError, match="does not support temperature"):
AnthropicMessagesRequestUtils.get_requested_anthropic_messages_optional_param(
params={"temperature": 0.3},
model="claude-opus-4-7",
drop_params=False,
)
Loading