diff --git a/src/google/adk/integrations/openai/_openai_llm.py b/src/google/adk/integrations/openai/_openai_llm.py index b2439e06f8d..a522c5f0890 100644 --- a/src/google/adk/integrations/openai/_openai_llm.py +++ b/src/google/adk/integrations/openai/_openai_llm.py @@ -282,18 +282,94 @@ def _usage_metadata( ) -def _tool_call_parts(message: ChatCompletionMessage) -> list[types.Part]: +def _parse_tool_call_arguments( + raw_arguments: Any, + name: str | None, + is_length_finish: bool, + malformed_names: list[str], +) -> dict[str, Any] | None: + """Parses tool call arguments into a JSON object, or None if invalid.""" + if not raw_arguments and not is_length_finish: + return {} + if raw_arguments and ( + not isinstance(raw_arguments, str) or raw_arguments.strip() + ): + try: + parsed = json.loads(raw_arguments) + if isinstance(parsed, dict): + return parsed + except (ValueError, RecursionError, TypeError): + pass + safe_name = name if isinstance(name, str) and name else "" + # Never log raw arguments: they can be large and carry user data. + logger.warning( + "Discarding tool call %r with malformed arguments (%d chars).", + safe_name, + len(raw_arguments) if isinstance(raw_arguments, str) else 0, + ) + malformed_names.append(safe_name) + return None + + +def _malformed_tool_call_response( + malformed_names: list[str], + parts: list[types.Part], + raw_finish_reason: str | None, + usage: CompletionUsage | None, + *, + streaming: bool = False, +) -> LlmResponse: + """Builds an error LlmResponse when tool call arguments are invalid.""" + finish_reason = _map_finish_reason(raw_finish_reason) + msg = ( + "Arguments for the following function calls were not a valid JSON" + f" object: {', '.join(malformed_names)}" + ) + if raw_finish_reason == "length": + error_code = types.FinishReason.MAX_TOKENS + msg = ( + "Tool call arguments were truncated and could not be parsed as valid" + " JSON. Increase `max_output_tokens` and retry." + ) + elif finish_reason in ( + None, + types.FinishReason.STOP, + types.FinishReason.FINISH_REASON_UNSPECIFIED, + ): + finish_reason = error_code = types.FinishReason.MALFORMED_FUNCTION_CALL + else: + error_code = finish_reason + msg = f"OpenAI response finished with reason {raw_finish_reason!r}. {msg}" + text = [p for p in parts if not p.function_call] + return LlmResponse( + content=types.Content(role="model", parts=text) if text else None, + partial=False if streaming else None, + error_code=error_code, + error_message=msg, + finish_reason=finish_reason, + usage_metadata=_usage_metadata(usage), + ) + + +def _tool_call_parts( + message: ChatCompletionMessage, + is_length_finish: bool = False, + malformed_names: list[str] | None = None, +) -> list[types.Part]: """Converts OpenAI tool calls on a message to ADK function-call parts.""" parts: list[types.Part] = [] + invalid: list[str] = [] for message_tool_call in message.tool_calls or []: # Only function tools are sent, so the model returns only function calls. tool_call = cast(ChatCompletionMessageFunctionToolCall, message_tool_call) - args = {} - if tool_call.function.arguments: - try: - args = json.loads(tool_call.function.arguments) - except json.JSONDecodeError: - logger.warning("Failed to parse tool call arguments as JSON.") + args = _parse_tool_call_arguments( + tool_call.function.arguments, + tool_call.function.name, + is_length_finish, + invalid, + ) + if args is None: + continue parts.append( types.Part( function_call=types.FunctionCall( @@ -301,7 +377,9 @@ def _tool_call_parts(message: ChatCompletionMessage) -> list[types.Part]: ) ) ) - return parts + if invalid and malformed_names is not None: + malformed_names.extend(invalid) + return [] if invalid else parts def _response_to_llm_response(response: ChatCompletion) -> LlmResponse: @@ -319,13 +397,20 @@ def _response_to_llm_response(response: ChatCompletion) -> LlmResponse: choice = response.choices[0] message = choice.message + raw_finish_reason = getattr(choice, "finish_reason", None) parts = [] if message.content: parts.append(types.Part.from_text(text=message.content)) - parts.extend(_tool_call_parts(message)) + malformed_names: list[str] = [] + parts.extend( + _tool_call_parts(message, raw_finish_reason == "length", malformed_names) + ) + if malformed_names: + return _malformed_tool_call_response( + malformed_names, parts, raw_finish_reason, usage + ) - raw_finish_reason = getattr(choice, "finish_reason", None) finish_reason = _map_finish_reason(raw_finish_reason) if not parts and finish_reason not in (None, types.FinishReason.STOP): @@ -604,16 +689,17 @@ async def _generate_content_streaming( if text_accumulated: parts.append(types.Part.from_text(text=text_accumulated)) + malformed_names: list[str] = [] for index in sorted(tool_calls_accumulated.keys()): acc = tool_calls_accumulated[index] - args = {} - if acc["arguments"]: - try: - args = json.loads(acc["arguments"]) - except json.JSONDecodeError: - logger.warning( - "Failed to parse accumulated tool call arguments as JSON." - ) + args = _parse_tool_call_arguments( + acc["arguments"], + acc["name"], + finish_reason == "length", + malformed_names, + ) + if args is None: + continue parts.append( types.Part( @@ -625,6 +711,12 @@ async def _generate_content_streaming( mapped_finish_reason = _map_finish_reason(finish_reason) + if malformed_names: + yield _malformed_tool_call_response( + malformed_names, parts, finish_reason, usage, streaming=True + ) + return + if not parts and mapped_finish_reason not in ( None, types.FinishReason.STOP, diff --git a/tests/unittests/integrations/openai/test_openai_llm.py b/tests/unittests/integrations/openai/test_openai_llm.py index 1badc484925..72e112d86f6 100644 --- a/tests/unittests/integrations/openai/test_openai_llm.py +++ b/tests/unittests/integrations/openai/test_openai_llm.py @@ -1701,3 +1701,313 @@ def test_usage_metadata_maps_reasoning_tokens(details, expected): metadata = _usage_metadata(usage) assert metadata.thoughts_token_count == expected assert metadata.candidates_token_count == 50 + + +def _tool_call_completion( + tool_calls, *, content=None, finish_reason="tool_calls" +): + """Builds a mock ChatCompletion carrying tool calls.""" + response = mock.MagicMock() + choice = mock.MagicMock() + message = mock.MagicMock() + message.content = content + mock_calls = [] + for call_id, name, arguments in tool_calls: + tc = mock.MagicMock() + tc.id = call_id + tc.function = mock.MagicMock() + tc.function.name = name + tc.function.arguments = arguments + mock_calls.append(tc) + message.tool_calls = mock_calls + choice.message = message + choice.finish_reason = finish_reason + response.choices = [choice] + response.usage.prompt_tokens = 10 + response.usage.completion_tokens = 5 + response.usage.total_tokens = 15 + response.usage.prompt_tokens_details = None + response.usage.completion_tokens_details = None + return response + + +def _tool_call_stream_chunk( + *, + index=0, + call_id=None, + name=None, + arguments=None, + content=None, + finish_reason=None, +): + """Builds a streaming chunk carrying a tool-call delta.""" + chunk = mock.MagicMock() + chunk.usage = None + choice = mock.MagicMock() + choice.finish_reason = finish_reason + delta = mock.MagicMock() + delta.content = content + if call_id is None and name is None and arguments is None: + delta.tool_calls = None + else: + tc = mock.MagicMock() + tc.index = index + tc.id = call_id + tc.function = mock.MagicMock() + tc.function.name = name + tc.function.arguments = arguments + delta.tool_calls = [tc] + choice.delta = delta + chunk.choices = [choice] + return chunk + + +async def _run_non_streaming_completion(mock_response): + """Runs one non-streaming OpenAILlm call returning mock_response.""" + openai_llm = OpenAILlm(model="gpt-4o", api_key="k") + llm_request = LlmRequest( + model="gpt-4o", + contents=[Content(role="user", parts=[Part.from_text(text="Run tool")])], + ) + + async def mock_create(*args, **kwargs): + return mock_response + + with mock.patch( + "google.adk.integrations.openai._openai_llm.AsyncOpenAI" + ) as mock_client_class: + mock_client = mock.MagicMock() + mock_client_class.return_value = mock_client + mock_client.chat.completions.create = mock_create + return [ + resp + async for resp in openai_llm.generate_content_async( + llm_request, stream=False + ) + ] + + +@pytest.mark.asyncio +async def test_non_streaming_truncated_tool_call_reports_max_tokens(): + """A non-streaming tool call cut off at length returns MAX_TOKENS, no call.""" + mock_response = _tool_call_completion( + [("call_1", "write_file", '{"path": "a.txt", "content": "hel')], + finish_reason="length", + ) + + responses = await _run_non_streaming_completion(mock_response) + + assert len(responses) == 1 + resp = responses[0] + assert resp.error_code == types.FinishReason.MAX_TOKENS + assert resp.finish_reason == types.FinishReason.MAX_TOKENS + assert "max_output_tokens" in resp.error_message + assert not resp.get_function_calls() + assert resp.content is None + + +@pytest.mark.asyncio +async def test_non_streaming_truncated_tool_call_keeps_text_and_drops_batch(): + """A truncated call drops the whole tool batch while preserving text.""" + mock_response = _tool_call_completion( + [ + ("call_1", "read_file", '{"path": "a.txt"}'), + ("call_2", "write_file", '{"path": "a.txt", "content": "hel'), + ], + content="Writing file.", + finish_reason="length", + ) + + responses = await _run_non_streaming_completion(mock_response) + + assert len(responses) == 1 + resp = responses[0] + assert resp.error_code == types.FinishReason.MAX_TOKENS + assert resp.finish_reason == types.FinishReason.MAX_TOKENS + assert not resp.get_function_calls() + assert [p.text for p in resp.content.parts] == ["Writing file."] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("arguments", ["[]", "null", '"x"']) +async def test_non_streaming_non_object_tool_call_reports_malformed(arguments): + """Non-object JSON tool arguments are rejected as MALFORMED_FUNCTION_CALL.""" + mock_response = _tool_call_completion( + [("call_1", "write_file", arguments)], + finish_reason="tool_calls", + ) + + responses = await _run_non_streaming_completion(mock_response) + + assert len(responses) == 1 + resp = responses[0] + assert resp.error_code == types.FinishReason.MALFORMED_FUNCTION_CALL + assert resp.finish_reason == types.FinishReason.MALFORMED_FUNCTION_CALL + assert "write_file" in resp.error_message + assert not resp.get_function_calls() + assert resp.content is None + + +@pytest.mark.asyncio +async def test_non_streaming_invalid_json_tool_call_reports_malformed(): + """Invalid JSON tool arguments with finish tool_calls report malformed.""" + mock_response = _tool_call_completion( + [("call_1", "write_file", '{"path": "a.txt"')], + content="Calling tool.", + finish_reason="tool_calls", + ) + + responses = await _run_non_streaming_completion(mock_response) + + assert len(responses) == 1 + resp = responses[0] + assert resp.error_code == types.FinishReason.MALFORMED_FUNCTION_CALL + assert resp.finish_reason == types.FinishReason.MALFORMED_FUNCTION_CALL + assert "write_file" in resp.error_message + assert not resp.get_function_calls() + assert [p.text for p in resp.content.parts] == ["Calling tool."] + + +@pytest.mark.asyncio +async def test_streaming_truncated_tool_call_reports_max_tokens(): + """Streamed argument deltas cut mid-JSON with finish length report MAX_TOKENS.""" + openai_llm = OpenAILlm(model="gpt-4o", api_key="k") + llm_request = LlmRequest( + model="gpt-4o", + contents=[Content(role="user", parts=[Part.from_text(text="Write")])], + ) + chunks = [ + _tool_call_stream_chunk( + call_id="call_1", + name="write_file", + arguments='{"path": "a.txt",', + ), + _tool_call_stream_chunk( + arguments=' "content": "hel', + finish_reason="length", + ), + _usage_only_chunk(), + ] + + with _stream_client(chunks): + responses = [ + resp + async for resp in openai_llm.generate_content_async( + llm_request, stream=True + ) + ] + + final = responses[-1] + assert final.partial is False + assert final.error_code == types.FinishReason.MAX_TOKENS + assert final.finish_reason == types.FinishReason.MAX_TOKENS + assert "max_output_tokens" in final.error_message + assert not final.get_function_calls() + assert final.content is None + assert final.usage_metadata.total_token_count == 20 + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "arguments", + ['{"path": "a.txt"', "[]", "null", '"x"'], +) +async def test_streaming_malformed_tool_call_reports_malformed(arguments): + """Streamed malformed or non-object tool arguments report malformed.""" + openai_llm = OpenAILlm(model="gpt-4o", api_key="k") + llm_request = LlmRequest( + model="gpt-4o", + contents=[Content(role="user", parts=[Part.from_text(text="Write")])], + ) + chunks = [ + _tool_call_stream_chunk( + call_id="call_1", + name="write_file", + arguments=arguments, + content="Calling tool.", + finish_reason="tool_calls", + ), + _usage_only_chunk(), + ] + + with _stream_client(chunks): + responses = [ + resp + async for resp in openai_llm.generate_content_async( + llm_request, stream=True + ) + ] + + final = responses[-1] + assert final.partial is False + assert final.error_code == types.FinishReason.MALFORMED_FUNCTION_CALL + assert final.finish_reason == types.FinishReason.MALFORMED_FUNCTION_CALL + assert "write_file" in final.error_message + assert not final.get_function_calls() + assert [p.text for p in final.content.parts] == ["Calling tool."] + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("arguments", "expected_args"), + [ + ('{"location": "NYC"}', {"location": "NYC"}), + ("", {}), + ("{}", {}), + ], +) +async def test_non_streaming_valid_and_empty_tool_call_arguments_succeed( + arguments, expected_args +): + """Valid JSON object and empty-string tool arguments still succeed.""" + mock_response = _tool_call_completion( + [("call_1", "get_weather", arguments)], + finish_reason="tool_calls", + ) + + responses = await _run_non_streaming_completion(mock_response) + + assert len(responses) == 1 + resp = responses[0] + assert resp.error_code is None + assert resp.finish_reason == types.FinishReason.STOP + calls = resp.get_function_calls() + assert len(calls) == 1 + assert calls[0].name == "get_weather" + assert calls[0].args == expected_args + + +@pytest.mark.asyncio +async def test_streaming_empty_tool_call_arguments_succeed(): + """Streamed no-argument tool call with empty arguments string yields {}.""" + openai_llm = OpenAILlm(model="gpt-4o", api_key="k") + llm_request = LlmRequest( + model="gpt-4o", + contents=[Content(role="user", parts=[Part.from_text(text="Ping")])], + ) + chunks = [ + _tool_call_stream_chunk( + call_id="call_1", + name="ping", + arguments="", + finish_reason="tool_calls", + ), + _usage_only_chunk(), + ] + + with _stream_client(chunks): + responses = [ + resp + async for resp in openai_llm.generate_content_async( + llm_request, stream=True + ) + ] + + final = responses[-1] + assert final.partial is False + assert final.error_code is None + assert final.finish_reason == types.FinishReason.STOP + calls = final.get_function_calls() + assert len(calls) == 1 + assert calls[0].name == "ping" + assert calls[0].args == {}