Skip to content
Merged
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
17 changes: 13 additions & 4 deletions src/agents/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,14 +1451,23 @@ def _on_handled_error(

def _parse_function_tool_json_input(*, tool_name: str, input_json: str) -> dict[str, Any]:
"""Decode raw tool arguments with consistent diagnostics."""
json_decode_error: Exception | None = None
try:
parsed = json.loads(input_json) if input_json else {}
except Exception as exc:
json_decode_error = exc

if json_decode_error is not None:
base_message = f"Invalid JSON input for tool {tool_name}"
if _debug.DONT_LOG_TOOL_DATA:
logger.debug(f"Invalid JSON input for tool {tool_name}")
else:
logger.debug(f"Invalid JSON input for tool {tool_name}: {input_json}")
raise ModelBehaviorError(f"Invalid JSON input for tool {tool_name}: {input_json}") from exc
logger.debug(base_message)
# Raise outside the ``except`` block so the JSONDecodeError, which
# carries the raw payload in ``.doc``, is not attached as the
# ``__context__`` of the redacted ModelBehaviorError.
raise ModelBehaviorError(base_message)
detailed_message = f"{base_message}: {input_json}"
logger.debug(detailed_message)
raise ModelBehaviorError(detailed_message) from json_decode_error

if not isinstance(parsed, dict):
raise ModelBehaviorError(f"Invalid JSON input for tool {tool_name}: expected a JSON object")
Expand Down
64 changes: 64 additions & 0 deletions tests/test_function_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import dataclasses
import json
import logging
import time
from collections.abc import Callable
from typing import Any, cast
Expand All @@ -11,6 +12,7 @@
from pydantic import BaseModel
from typing_extensions import TypedDict

import agents._debug as _debug
import agents.tool as tool_module
from agents import (
Agent,
Expand Down Expand Up @@ -780,6 +782,68 @@ def echo(value: str) -> str:
)


@pytest.mark.asyncio
async def test_function_tool_bad_json_redacts_payload_when_dont_log_tool_data(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
caplog.set_level(logging.DEBUG)
monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", True)

def echo(value: str) -> str:
return value

tool = function_tool(echo, name_override="echo_tool", failure_error_function=None)
bad_json = '{"secret":"SECRET_TOKEN_123"'

with pytest.raises(ModelBehaviorError) as exc_info:
await tool.on_invoke_tool(
ToolContext(
None,
tool_name="echo_tool",
tool_call_id="1",
tool_arguments=bad_json,
),
bad_json,
)

assert str(exc_info.value) == "Invalid JSON input for tool echo_tool"
assert exc_info.value.__cause__ is None
assert exc_info.value.__context__ is None
assert "SECRET_TOKEN_123" not in str(exc_info.value)
assert "SECRET_TOKEN_123" not in caplog.text


@pytest.mark.asyncio
async def test_function_tool_bad_json_includes_payload_when_tool_logging_enabled(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
caplog.set_level(logging.DEBUG)
monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", False)

def echo(value: str) -> str:
return value

tool = function_tool(echo, name_override="echo_tool", failure_error_function=None)
bad_json = '{"secret":"SECRET_TOKEN_123"'

with pytest.raises(ModelBehaviorError) as exc_info:
await tool.on_invoke_tool(
ToolContext(
None,
tool_name="echo_tool",
tool_call_id="1",
tool_arguments=bad_json,
),
bad_json,
)

assert str(exc_info.value) == f"Invalid JSON input for tool echo_tool: {bad_json}"
assert isinstance(exc_info.value.__cause__, json.JSONDecodeError)
assert exc_info.value.__cause__.doc == bad_json
assert "SECRET_TOKEN_123" in str(exc_info.value)
assert "SECRET_TOKEN_123" in caplog.text


@pytest.mark.asyncio
async def test_default_failure_error_function_survives_deepcopy() -> None:
def boom() -> None:
Expand Down
7 changes: 6 additions & 1 deletion tests/test_run_step_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
TResponseInputItem,
Usage,
UserError,
_debug,
tool_namespace,
tool_output_guardrail,
trace,
Expand Down Expand Up @@ -2814,7 +2815,11 @@ async def test_multiple_final_output_leads_to_final_output_next_step():


@pytest.mark.asyncio
async def test_input_guardrail_runs_on_invalid_json():
async def test_input_guardrail_runs_on_invalid_json(monkeypatch: pytest.MonkeyPatch):
# Opt in to payload logging so the JSON decode error chain is preserved and the
# default failure formatter can recover the friendly "parsing tool arguments" message.
monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", False)

guardrail_calls: list[str] = []

def guardrail(data) -> ToolGuardrailFunctionOutput:
Expand Down
7 changes: 6 additions & 1 deletion tests/test_tracing_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
RunContextWrapper,
Runner,
TResponseInputItem,
_debug,
)

from .fake_model import FakeModel
Expand Down Expand Up @@ -133,7 +134,11 @@ async def test_multi_turn_no_handoffs():


@pytest.mark.asyncio
async def test_tool_call_error():
async def test_tool_call_error(monkeypatch: pytest.MonkeyPatch):
# Opt in to tool payload logging so the friendly "parsing tool arguments" message,
# which depends on inspecting the chained JSONDecodeError, is preserved.
monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", False)

model = FakeModel(tracing_enabled=True)

agent = Agent(
Expand Down
7 changes: 6 additions & 1 deletion tests/test_tracing_errors_streamed.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
RunContextWrapper,
Runner,
TResponseInputItem,
_debug,
)

from .fake_model import FakeModel
Expand Down Expand Up @@ -160,7 +161,11 @@ async def test_multi_turn_no_handoffs():


@pytest.mark.asyncio
async def test_tool_call_error():
async def test_tool_call_error(monkeypatch: pytest.MonkeyPatch):
# Opt in to tool payload logging so the friendly "parsing tool arguments" message,
# which depends on inspecting the chained JSONDecodeError, is preserved.
monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", False)

model = FakeModel(tracing_enabled=True)

agent = Agent(
Expand Down
Loading