fix(bedrock): omit sampling params for restricted Claude models

Bedrock Converse rejects non-default sampling parameters for Opus 4.7 and 4.8 with a ValidationException. Reuse the Anthropic-native sampling-param guard in the Bedrock kwargs builder so those models omit temperature/topP while older Claude and non-Claude models keep existing behavior.

Includes the stop-sequence regression from the parallel fix to ensure stopSequences still pass through for restricted Opus models.

Co-authored-by: Tranquil-Flow <tranquil_flow@protonmail.com>
This commit is contained in:
ashishpatel26 2026-06-13 07:25:04 -07:00 committed by Teknium
parent cc14b74718
commit 957a8ffa88
2 changed files with 75 additions and 4 deletions

View file

@ -935,11 +935,14 @@ def build_converse_kwargs(
if system_prompt:
kwargs["system"] = system_prompt
if temperature is not None:
kwargs["inferenceConfig"]["temperature"] = temperature
from agent.anthropic_adapter import _forbids_sampling_params
if top_p is not None:
kwargs["inferenceConfig"]["topP"] = top_p
if not _forbids_sampling_params(model):
if temperature is not None:
kwargs["inferenceConfig"]["temperature"] = temperature
if top_p is not None:
kwargs["inferenceConfig"]["topP"] = top_p
if stop_sequences:
kwargs["inferenceConfig"]["stopSequences"] = stop_sequences

View file

@ -605,6 +605,74 @@ class TestBuildConverseKwargs:
assert kwargs["inferenceConfig"]["temperature"] == 0.7
assert kwargs["inferenceConfig"]["topP"] == 0.9
def test_omits_sampling_params_for_bedrock_opus_4_7(self):
from agent.bedrock_adapter import build_converse_kwargs
for model_id in (
"anthropic.claude-opus-4-7-20260101-v1:0",
"us.anthropic.claude-opus-4-7",
):
kwargs = build_converse_kwargs(
model=model_id,
messages=[{"role": "user", "content": "Hi"}],
temperature=0.7,
top_p=0.9,
)
assert "temperature" not in kwargs["inferenceConfig"]
assert "topP" not in kwargs["inferenceConfig"]
def test_omits_sampling_params_for_bedrock_opus_4_8_variants(self):
from agent.bedrock_adapter import build_converse_kwargs
for model_id in (
"anthropic.claude-opus-4-8-20270101-v1:0",
"us.anthropic.claude-opus-4-8",
"anthropic.claude-opus-4.8",
):
kwargs = build_converse_kwargs(
model=model_id,
messages=[{"role": "user", "content": "Hi"}],
temperature=0.5,
top_p=0.95,
)
assert "temperature" not in kwargs["inferenceConfig"]
assert "topP" not in kwargs["inferenceConfig"]
def test_keeps_sampling_params_for_bedrock_non_restricted_models(self):
from agent.bedrock_adapter import build_converse_kwargs
for model_id in (
"anthropic.claude-sonnet-4-6-20250514-v1:0",
"anthropic.claude-haiku-4-5",
"test-model",
):
kwargs = build_converse_kwargs(
model=model_id,
messages=[{"role": "user", "content": "Hi"}],
temperature=0.7,
top_p=0.9,
)
assert kwargs["inferenceConfig"].get("temperature") == 0.7
assert kwargs["inferenceConfig"].get("topP") == 0.9
def test_bedrock_opus_strips_sampling_params_but_keeps_stop_sequences(self):
from agent.bedrock_adapter import build_converse_kwargs
kwargs = build_converse_kwargs(
model="us.anthropic.claude-opus-4-8",
messages=[{"role": "user", "content": "Hi"}],
temperature=0.7,
top_p=0.9,
stop_sequences=["END"],
)
assert "temperature" not in kwargs["inferenceConfig"]
assert "topP" not in kwargs["inferenceConfig"]
assert kwargs["inferenceConfig"]["stopSequences"] == ["END"]
def test_includes_guardrail_config(self):
from agent.bedrock_adapter import build_converse_kwargs
guardrail = {