Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ def _produce():
if aggregated_text:
final_parts.append(types.Part.from_text(text=aggregated_text))
for tool_id, tool in tool_uses.items():
args = json.loads(tool["input_json"]) if tool["input_json"] else {}
try:
args = json.loads(tool["input_json"]) if tool["input_json"] else {}
except json.JSONDecodeError:
args = {}
part = types.Part.from_function_call(name=tool["name"], args=args)
if part.function_call:
part.function_call.id = tool_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,42 @@ async def fake_to_thread(fn, **kwargs):
tool_names = [t["toolSpec"]["name"] for t in call_kwargs["toolConfig"]["tools"]]
assert tool_names == ["fetch_get_url"]

@pytest.mark.asyncio
async def test_streaming_malformed_tool_input_degrades_to_empty_args(self):
"""A truncated/malformed streamed tool-argument JSON must not abort the
turn: it degrades to empty args, matching the OpenAI and SAP AI Core
streaming paths."""
llm = KAgentBedrockLlm(model="us.anthropic.claude-sonnet-4-20250514-v1:0")

stream_events = [
{"contentBlockStart": {"start": {"toolUse": {"toolUseId": "call-xyz", "name": "get_weather"}}}},
{"contentBlockDelta": {"delta": {"toolUse": {"input": '{"city": "Paris"'}}}},
{"messageStop": {"stopReason": "tool_use"}},
]
mock_client = mock.MagicMock()
mock_client.converse_stream.return_value = {"stream": stream_events}

async def fake_to_thread(fn, **kwargs):
return fn(**kwargs)

request = mock.MagicMock()
request.model = "us.anthropic.claude-sonnet-4-20250514-v1:0"
request.contents = []
request.config = None

with (
mock.patch("kagent.adk.models._bedrock._get_bedrock_client", return_value=mock_client),
mock.patch("kagent.adk.models._bedrock.asyncio.to_thread", side_effect=fake_to_thread),
):
responses = [r async for r in llm.generate_content_async(request, stream=True)]
Comment on lines +324 to +336

final = responses[-1]
assert final.error_code is None
fc = final.content.parts[0].function_call
assert fc.name == "get_weather"
assert fc.id == "call-xyz"
assert fc.args == {}

def test_create_llm_from_bedrock_model_config(self):
from kagent.adk.types import Bedrock, _create_llm_from_model_config

Expand Down
Loading