mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-04 02:31:27 +00:00
fix(bedrock): preserve native stop reason in assembled streams
This commit is contained in:
parent
da5699220b
commit
cf2faa852a
2 changed files with 29 additions and 0 deletions
|
|
@ -391,6 +391,15 @@ class ChunkProcessor:
|
|||
if chunk_finish_reason is not None:
|
||||
finish_reason = chunk_finish_reason
|
||||
|
||||
choice_provider_specific_fields: Final[dict[str, object]] = { # mutable-ok: response field requires a dict
|
||||
key: value
|
||||
for chunk in chunks
|
||||
if chunk.get("choices")
|
||||
for fields in (chunk["choices"][0].get("provider_specific_fields"),)
|
||||
if isinstance(fields, dict)
|
||||
for key, value in fields.items()
|
||||
}
|
||||
|
||||
# Initialize the response dictionary
|
||||
response = ModelResponse(
|
||||
**{
|
||||
|
|
@ -404,6 +413,7 @@ class ChunkProcessor:
|
|||
"index": 0,
|
||||
"message": {"role": role, "content": ""},
|
||||
"finish_reason": finish_reason,
|
||||
"provider_specific_fields": choice_provider_specific_fields or None,
|
||||
}
|
||||
],
|
||||
"usage": {
|
||||
|
|
|
|||
|
|
@ -4629,6 +4629,8 @@ def test_transform_response_native_structured_output():
|
|||
assert result.choices[0].finish_reason == "stop"
|
||||
|
||||
|
||||
# AWS Bedrock Converse stopReason values, accessed 2026-09-16:
|
||||
# https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html
|
||||
@pytest.mark.parametrize("stop_reason", ("stop_sequence", "end_turn"))
|
||||
def test_transform_response_preserves_raw_bedrock_stop_reason(stop_reason: str):
|
||||
response_json = {
|
||||
|
|
@ -4670,6 +4672,8 @@ def test_transform_response_preserves_raw_bedrock_stop_reason(stop_reason: str):
|
|||
}
|
||||
|
||||
|
||||
# AWS Bedrock Converse stopReason values, accessed 2026-09-16:
|
||||
# https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html
|
||||
@pytest.mark.parametrize("stop_reason", ("stop_sequence", "end_turn"))
|
||||
def test_converse_chunk_parser_preserves_raw_bedrock_stop_reason(stop_reason: str):
|
||||
result = AWSEventStreamDecoder(model="bedrock/claude").converse_chunk_parser(
|
||||
|
|
@ -4682,6 +4686,21 @@ def test_converse_chunk_parser_preserves_raw_bedrock_stop_reason(stop_reason: st
|
|||
}
|
||||
|
||||
|
||||
def test_converse_assembled_stream_preserves_raw_bedrock_stop_reason():
|
||||
# AWS Bedrock Converse stopReason value, accessed 2026-09-16:
|
||||
# https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html
|
||||
result = AWSEventStreamDecoder(model="bedrock/claude").converse_chunk_parser(
|
||||
{"stopReason": "stop_sequence"}
|
||||
)
|
||||
|
||||
assembled = litellm.stream_chunk_builder(chunks=[result])
|
||||
|
||||
assert assembled is not None
|
||||
assert assembled.choices[0].provider_specific_fields == {
|
||||
"native_finish_reason": "stop_sequence"
|
||||
}
|
||||
|
||||
|
||||
def test_add_additional_properties_simple_object():
|
||||
"""Object schemas without additionalProperties get it set to false."""
|
||||
schema = {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue