mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* fix(bedrock): send native structured output first on Opus 4.8 and Sonnet 5 and fall back to the json tool when Bedrock rejects it
* fix(bedrock): retry with the json tool when Bedrock refuses the native output schema
Count a 400 under output_config.format.schema: as a native output rejection, so a schema Bedrock cannot compile (like a pattern with a nested {n,m} group) gets the json tool retry instead of reaching the caller
* fix(bedrock): make the native output retry send what an opted-out deployment sends
The retry after a native output rejection now reruns the same optional param
mapping completion() ran, with the deployment model_info opted out of
supports_bedrock_native_structured_output, so tool_choice, effort and the json
tool schema match an opted-out deployment instead of a hand-built tool. Each
config keeps only the predicate that decides when native output came from the
opt-in key.
Bedrock batch records for Anthropic models map with the opt-in turned off,
since a batch job has no retry. Models on supports_native_structured_output
keep native output in batch
* fix(bedrock): build the messages retry from the caller's request and keep other errors unchanged
* fix(bedrock): give sync converse stream errors the upstream body as text
* fix(bedrock): let a malformed caller outputConfig reach Bedrock instead of raising
A Converse json_schema request whose caller outputConfig failed litellm's
shape check raised a pydantic error before the request was sent, so the
caller got a 500 where the merge base passed Bedrock's 400 through. The
opt-in check now treats that outputConfig as carrying no native output.
Also opts the vertex wire test's pinned Opus 4.8 json tool deployment out of
native output, and extends the Bedrock native output integration tests.
* fix(bedrock): send a refused caller outputConfig once and give opt-out advice only when native output is unsupported
A caller can send its own outputConfig on Converse. When Bedrock refused that
schema on an opt-in deployment, the retry still carried the caller's textFormat,
so Bedrock got the same request twice and the warning told the operator to turn
off an opt-in that was not the cause. The retry is now skipped when the opted-out
request would still carry a native textFormat, so Bedrock's 400 reaches the
caller after one call as on the merge base. The opt-out advice is logged only for
the account-level "Extra inputs are not permitted" rejection.
* fix(bedrock): keep the caller's continuation message on the native output retry
The Converse transform pops user_continue_message from litellm_params on the first attempt, so a retry built from those params afterwards dropped it. Both Bedrock chat routes now hand the fallback a copy taken before the first attempt.
* test(bedrock): pin additional_drop_params on the /v1/messages native output retry
* fix(bedrock): give the Converse native output retry the fake stream timeout
131 lines
5.2 KiB
Python
131 lines
5.2 KiB
Python
import json
|
|
from typing import Final
|
|
|
|
import pytest
|
|
from integration._support.client import JSON_OBJECT, Gateway, list_value, object_value
|
|
from integration._support.wire import Reply, Request, wire_server
|
|
|
|
MODEL: Final = "bedrock/anthropic.claude-3-haiku-20240307-v1:0"
|
|
BUCKET: Final = "integration-batch-bucket"
|
|
PROMPT: Final = "synthetic completions prompt"
|
|
RESPONSES_INPUT: Final = "synthetic responses input"
|
|
INPUT_LINES: Final = (
|
|
{
|
|
"custom_id": "completions-record",
|
|
"method": "POST",
|
|
"url": "/v1/completions",
|
|
"body": {"model": MODEL, "prompt": PROMPT, "max_tokens": 64},
|
|
},
|
|
{
|
|
"custom_id": "responses-record",
|
|
"method": "POST",
|
|
"url": "/v1/responses",
|
|
"body": {"model": MODEL, "input": RESPONSES_INPUT, "max_output_tokens": 16},
|
|
},
|
|
)
|
|
EXPECTED_S3_OBJECT: Final = (
|
|
{
|
|
"recordId": "completions-record",
|
|
"modelInput": {
|
|
"messages": [{"role": "user", "content": [{"type": "text", "text": PROMPT}]}],
|
|
"max_tokens": 64,
|
|
"anthropic_version": "bedrock-2023-05-31",
|
|
},
|
|
},
|
|
{
|
|
"recordId": "responses-record",
|
|
"modelInput": {
|
|
"messages": [{"role": "user", "content": [{"type": "text", "text": RESPONSES_INPUT}]}],
|
|
"max_tokens": 16,
|
|
"anthropic_version": "bedrock-2023-05-31",
|
|
},
|
|
},
|
|
)
|
|
|
|
|
|
def s3_peer(request: Request) -> Reply:
|
|
assert request.method == "PUT" and request.target.startswith(f"/{BUCKET}/"), request.target
|
|
assert request.headers["authorization"].startswith("AWS4-HMAC-SHA256 ")
|
|
return Reply(body=b"")
|
|
|
|
|
|
@pytest.mark.covers(
|
|
"other.provider_wire.bedrock.batch_file_completions_and_responses_records_reach_s3_as_user_messages"
|
|
)
|
|
def test_completions_and_responses_batch_records_upload_as_anthropic_user_messages(gateway: Gateway) -> None:
|
|
with wire_server(s3_peer) as wire, gateway.scenario() as scenario:
|
|
model: Final = scenario.model(
|
|
model=MODEL,
|
|
api_key=None,
|
|
api_base=None,
|
|
aws_access_key_id="AKIAIOSFODNN7EXAMPLE",
|
|
aws_secret_access_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
|
aws_region_name="us-east-1",
|
|
s3_bucket_name=BUCKET,
|
|
s3_endpoint_url=wire.url,
|
|
)
|
|
jsonl: Final = "\n".join(json.dumps(line, separators=(",", ":")) for line in INPUT_LINES) + "\n"
|
|
response: Final = gateway.request_multipart(
|
|
"/v1/files",
|
|
{"purpose": "batch", "model": model},
|
|
{"file": ("in.jsonl", jsonl.encode(), "application/jsonl")},
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
assert response.json()["object"] == "file" and response.json()["purpose"] == "batch", response.text
|
|
uploads: Final = wire.drain()
|
|
assert len(uploads) == 1, f"Expected exactly one S3 PUT, saw {[upload.target for upload in uploads]}"
|
|
stored: Final = tuple(json.loads(line) for line in uploads[0].body.decode().splitlines() if line.strip())
|
|
assert stored == EXPECTED_S3_OBJECT
|
|
|
|
|
|
_JSON_TOOL: Final = "json_tool_call"
|
|
_SCHEMA: Final = {
|
|
"type": "object",
|
|
"properties": {"city": {"type": "string"}},
|
|
"required": ["city"],
|
|
"additionalProperties": False,
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"backend", ["us.anthropic.claude-haiku-5-5", "us.anthropic.claude-opus-4-8"], ids=["haiku_5_5", "opus_4_8"]
|
|
)
|
|
def test_json_schema_batch_record_with_thinking_uploads_the_json_tool_and_no_native_output(
|
|
gateway: Gateway, backend: str
|
|
) -> None:
|
|
record: Final = {
|
|
"custom_id": "schema-record",
|
|
"method": "POST",
|
|
"url": "/v1/chat/completions",
|
|
"body": {
|
|
"model": f"bedrock/{backend}",
|
|
"messages": [{"role": "user", "content": "Name a city."}],
|
|
"max_tokens": 2048,
|
|
"thinking": {"type": "adaptive"},
|
|
"response_format": {"type": "json_schema", "json_schema": {"name": "city", "schema": _SCHEMA}},
|
|
},
|
|
}
|
|
with wire_server(s3_peer) as wire, gateway.scenario() as scenario:
|
|
model: Final = scenario.model(
|
|
model=f"bedrock/{backend}",
|
|
api_key=None,
|
|
api_base=None,
|
|
aws_access_key_id="AKIAIOSFODNN7EXAMPLE",
|
|
aws_secret_access_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
|
aws_region_name="us-east-1",
|
|
s3_bucket_name=BUCKET,
|
|
s3_endpoint_url=wire.url,
|
|
)
|
|
response: Final = gateway.request_multipart(
|
|
"/v1/files",
|
|
{"purpose": "batch", "model": model},
|
|
{"file": ("in.jsonl", (json.dumps(record) + "\n").encode(), "application/jsonl")},
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
(upload,) = wire.drain()
|
|
(stored,) = tuple(JSON_OBJECT.validate_json(line) for line in upload.body.decode().splitlines() if line.strip())
|
|
model_input: Final = object_value(stored["modelInput"])
|
|
assert "format" not in object_value(model_input.get("output_config", {})), model_input
|
|
(tool,) = (object_value(entry) for entry in list_value(model_input["tools"]))
|
|
assert tool["name"] == _JSON_TOOL, model_input
|
|
assert object_value(tool["input_schema"])["properties"] == _SCHEMA["properties"], model_input
|