diff --git a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py index 67684a230e3..03de1884f92 100644 --- a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py +++ b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py @@ -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": { diff --git a/tests/unit/llms/bedrock/chat/test_converse_transformation.py b/tests/unit/llms/bedrock/chat/test_converse_transformation.py index 4d8a38882d3..19e67bf6a0a 100644 --- a/tests/unit/llms/bedrock/chat/test_converse_transformation.py +++ b/tests/unit/llms/bedrock/chat/test_converse_transformation.py @@ -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 = {