Skip to content

Commit 7ff000f

Browse files
authored
fix: type error during streaming of AmazonBedrockChatGenerator (#3332)
1 parent 376f2cf commit 7ff000f

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

integrations/amazon_bedrock/src/haystack_integrations/components/generators/amazon_bedrock/chat/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,13 +569,16 @@ def _convert_event_to_streaming_chunk(
569569
# This only occurs when accumulating the arguments for a toolUse
570570
# The content_block for this tool should already exist at this point
571571
elif "toolUse" in delta:
572+
tool_use_input = delta["toolUse"].get("input", "")
573+
# boto3 might return int for input
574+
arguments = str(tool_use_input) if tool_use_input is not None else None
572575
streaming_chunk = StreamingChunk(
573576
content="",
574577
index=block_idx,
575578
tool_calls=[
576579
ToolCallDelta(
577580
index=block_idx,
578-
arguments=delta["toolUse"].get("input", ""),
581+
arguments=arguments,
579582
)
580583
],
581584
meta=base_meta,

integrations/amazon_bedrock/tests/test_chat_generator_utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from haystack.tools import Tool
1818

1919
from haystack_integrations.components.generators.amazon_bedrock.chat.utils import (
20+
_convert_event_to_streaming_chunk,
2021
_convert_file_content_to_bedrock_format,
2122
_convert_streaming_chunks_to_chat_message,
2223
_format_messages,
@@ -2099,3 +2100,42 @@ def test_validate_and_format_cache_point(self):
20992100

21002101
with pytest.raises(ValueError, match=r"Cache point can only contain 'type' and 'ttl' keys."):
21012102
_validate_and_format_cache_point({"type": "default", "invalid": "config"})
2103+
2104+
def test_convert_event_to_streaming_chunk_tool_use_input_int(self):
2105+
model = "global.anthropic.claude-sonnet-4-6"
2106+
component_info = ComponentInfo(
2107+
type="haystack_integrations.components.generators.amazon_bedrock.chat.chat_generator.AmazonBedrockChatGenerator"
2108+
)
2109+
2110+
# boto3 may return an int for toolUse input — must be cast to str
2111+
event_int = {
2112+
"contentBlockDelta": {
2113+
"delta": {"toolUse": {"input": 42}},
2114+
"contentBlockIndex": 1,
2115+
}
2116+
}
2117+
chunk = _convert_event_to_streaming_chunk(event_int, model, component_info)
2118+
assert chunk.tool_calls is not None
2119+
assert chunk.tool_calls[0].arguments == "42"
2120+
2121+
# None input should stay None
2122+
event_none = {
2123+
"contentBlockDelta": {
2124+
"delta": {"toolUse": {"input": None}},
2125+
"contentBlockIndex": 1,
2126+
}
2127+
}
2128+
chunk = _convert_event_to_streaming_chunk(event_none, model, component_info)
2129+
assert chunk.tool_calls is not None
2130+
assert chunk.tool_calls[0].arguments is None
2131+
2132+
# Normal string input should pass through unchanged
2133+
event_str = {
2134+
"contentBlockDelta": {
2135+
"delta": {"toolUse": {"input": '{"city": "Berlin"}'}},
2136+
"contentBlockIndex": 1,
2137+
}
2138+
}
2139+
chunk = _convert_event_to_streaming_chunk(event_str, model, component_info)
2140+
assert chunk.tool_calls is not None
2141+
assert chunk.tool_calls[0].arguments == '{"city": "Berlin"}'

0 commit comments

Comments
 (0)