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
39 changes: 26 additions & 13 deletions src/agents/extensions/experimental/codex/codex_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ class CodexToolOptions:
failure_error_function: ToolErrorFunction | None = default_tool_error_function
use_run_context_thread_id: bool = False
run_context_thread_id_key: str | None = None
parameter_validation_mode: Literal["python", "json"] = "python"
"""Use "json" for strict types such as tuples. The default validates decoded Python values."""


class CodexToolCallArguments(TypedDict):
Expand Down Expand Up @@ -256,7 +258,9 @@ def codex_tool(
failure_error_function: ToolErrorFunction | None | _UnsetType = _UNSET,
use_run_context_thread_id: bool | None = None,
run_context_thread_id_key: str | None = None,
parameter_validation_mode: Literal["python", "json"] | None = None,
) -> FunctionTool:
"""Create a Codex tool. Set parameter_validation_mode="json" for Pydantic's JSON input mode."""
resolved_options = _coerce_tool_options(options)
if name is not None:
resolved_options.name = name
Expand Down Expand Up @@ -296,6 +300,10 @@ def codex_tool(
resolved_options.use_run_context_thread_id = use_run_context_thread_id
if run_context_thread_id_key is not None:
resolved_options.run_context_thread_id_key = run_context_thread_id_key
if parameter_validation_mode is None:
parameter_validation_mode = resolved_options.parameter_validation_mode
if parameter_validation_mode not in ("python", "json"):
raise UserError('parameter_validation_mode must be "python" or "json".')
resolved_options.codex_options = coerce_codex_options(resolved_options.codex_options)
resolved_options.default_thread_options = coerce_thread_options(
resolved_options.default_thread_options
Expand Down Expand Up @@ -338,7 +346,7 @@ async def _on_invoke_tool(ctx: ToolContext[Any], input_json: str) -> Any:
nonlocal persisted_thread
resolved_thread_id: str | None = None
try:
parsed = _parse_tool_input(parameters_model, input_json)
parsed = _parse_tool_input(parameters_model, input_json, parameter_validation_mode)
args = _normalize_parameters(parsed)

if resolved_options.use_run_context_thread_id:
Expand Down Expand Up @@ -530,25 +538,30 @@ def _validate_default_run_context_thread_id_suffix(value: str) -> str:
return suffix


def _parse_tool_input(parameters_model: type[BaseModel], input_json: str) -> BaseModel:
def _parse_tool_input(
parameters_model: type[BaseModel], input_json: str, mode: Literal["python", "json"]
) -> BaseModel:
base_message = "Invalid JSON input for codex tool"
decode_failed = False
try:
json_data = json.loads(input_json) if input_json else {}
except Exception as exc:
if not _debug.DONT_LOG_TOOL_DATA:
logger.debug("%s: %s", base_message, input_json)
raise ModelBehaviorError(f"{base_message}: {input_json}") from exc
logger.debug(base_message)
decode_failed = True

if decode_failed:
def decode_json() -> object:
try:
return json.loads(input_json) if input_json else {}
except Exception as exc:
if not _debug.DONT_LOG_TOOL_DATA:
logger.debug("%s: %s", base_message, input_json)
raise ModelBehaviorError(f"{base_message}: {input_json}") from exc
logger.debug(base_message)
raise ModelBehaviorError(base_message)

try:
return parameters_model.model_validate(json_data)
if mode == "json":
return parameters_model.model_validate_json(input_json or "{}")
return parameters_model.model_validate(decode_json())
except ValidationError as exc:
if not _debug.DONT_LOG_TOOL_DATA:
if mode == "json":
# Retain the distinct JSONDecodeError cause for malformed diagnostic input.
decode_json()
raise ModelBehaviorError(f"{base_message}: {exc}") from exc

raise ModelBehaviorError(base_message)
Expand Down
105 changes: 101 additions & 4 deletions tests/extensions/experiemental/codex/test_codex_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import inspect
import json
from dataclasses import dataclass, fields
from enum import Enum
from pathlib import Path
from types import MappingProxyType, SimpleNamespace
from typing import Any, cast
from typing import Any, Literal, cast

import pytest
from openai.types.responses import ResponseFunctionToolCall
from pydantic import BaseModel, ConfigDict, ValidationError
from pydantic import BaseModel, ConfigDict, ValidationError, ValidationInfo, field_validator

import agents._debug as _debug
from agents import Agent, Runner, function_tool
Expand Down Expand Up @@ -46,6 +48,7 @@ def __init__(self) -> None:
self.events: list[dict[str, Any]] = []
self.thread_id: str | None = "thread-1"
self.last_turn_options: Any = None
self.last_input: Any = None
self.start_calls = 0
self.resume_calls = 0
self.last_resumed_thread_id: str | None = None
Expand All @@ -57,7 +60,8 @@ def __init__(self, state: CodexMockState) -> None:
self._state = state
self.id: str | None = None

async def run_streamed(self, _input: Any, turn_options: Any = None) -> Any:
async def run_streamed(self, input: Any, turn_options: Any = None) -> Any:
self._state.last_input = input
self._state.last_turn_options = turn_options
self.id = self._state.thread_id

Expand Down Expand Up @@ -1480,7 +1484,7 @@ def test_codex_tool_result_stringifies() -> None:

def test_codex_tool_parse_input_rejects_invalid_json() -> None:
with pytest.raises(ModelBehaviorError, match="Invalid JSON input for codex tool"):
codex_tool_module._parse_tool_input(codex_tool_module.CodexToolParameters, "{bad")
codex_tool_module._parse_tool_input(codex_tool_module.CodexToolParameters, "{bad", "python")


def test_codex_tool_normalize_parameters_requires_inputs() -> None:
Expand All @@ -1496,6 +1500,11 @@ def test_codex_tool_coerce_options_rejects_unknown_fields() -> None:
codex_tool_module._coerce_tool_options({"unknown": "value"})


def test_codex_tool_rejects_unknown_parameter_validation_mode() -> None:
with pytest.raises(UserError, match="parameter_validation_mode"):
codex_tool({"parameter_validation_mode": "invalid"})


def test_codex_tool_keyword_rejects_empty_run_context_key() -> None:
with pytest.raises(UserError, match="run_context_thread_id_key"):
codex_tool(run_context_thread_id_key=" ")
Expand Down Expand Up @@ -2014,6 +2023,91 @@ def test_codex_tool_resolve_codex_options_reads_env(monkeypatch: pytest.MonkeyPa
assert resolved.api_key == "env-key"


class _RegisteredThread(str, Enum):
SUMMARIES = "thread-7"


class _StrictTupleParameters(BaseModel):
model_config = ConfigDict(strict=True)
inputs: tuple[CodexToolInputItem, ...]
thread_id: _RegisteredThread | Literal["thread-archive"]


class _UnionParameters(BaseModel):
inputs: list[CodexToolInputItem]
thread_id: Path | str


@pytest.mark.parametrize(
"parameters, options",
[
pytest.param(
_StrictTupleParameters, {"parameter_validation_mode": "json"}, id="strict-json"
),
pytest.param(_UnionParameters, None, id="default-python-union"),
],
)
@pytest.mark.asyncio
async def test_codex_tool_invokes_thread_with_custom_parameters(
parameters: type[BaseModel],
options: dict[str, str] | None,
) -> None:
state = CodexMockState()
tool = codex_tool(
options,
parameters=parameters,
codex=cast(Codex, FakeCodex(state)),
failure_error_function=None,
)
input_json = (
'{"inputs":[{"type":"text","text":"Summarize","path":null}],"thread_id":"thread-7"}'
)
context = ToolContext(
None, tool_name=tool.name, tool_call_id="call-1", tool_arguments=input_json
)

await tool.on_invoke_tool(context, input_json)

assert state.last_input == [{"type": "text", "text": "Summarize"}]
assert state.last_resumed_thread_id == "thread-7"


@pytest.mark.parametrize("mode", ["python", "json"])
@pytest.mark.asyncio
async def test_codex_tool_does_not_retry_a_rejected_thread_id(
mode: Literal["python", "json"],
) -> None:
thread_lookups: list[tuple[str, str]] = []

class RegisteredThreadParameters(BaseModel):
inputs: list[CodexToolInputItem]
thread_id: str

@field_validator("thread_id")
@classmethod
def require_registered_thread(cls, value: str, info: ValidationInfo) -> str:
thread_lookups.append((info.mode, value))
raise ValueError("Unknown thread")

state = CodexMockState()
tool = codex_tool(
parameters=RegisteredThreadParameters,
parameter_validation_mode=mode,
codex=cast(Codex, FakeCodex(state)),
failure_error_function=None,
)
input_json = '{"inputs":[{"type":"text","text":"Summarize"}],"thread_id":"unknown"}'
context = ToolContext(
None, tool_name=tool.name, tool_call_id="call-1", tool_arguments=input_json
)

with pytest.raises(ModelBehaviorError):
await tool.on_invoke_tool(context, input_json)

assert thread_lookups == [(mode, "unknown")]
assert state.start_calls == state.resume_calls == 0


def test_codex_tool_accepts_all_keyword_overrides() -> None:
state = CodexMockState()

Expand Down Expand Up @@ -2076,18 +2170,21 @@ def test_codex_tool_coerce_options_rejects_empty_run_context_key() -> None:
ids=["validation", "json_decode"],
)
@pytest.mark.parametrize("redact", [True, False], ids=["redacted", "diagnostic"])
@pytest.mark.parametrize("mode", ["python", "json"])
@pytest.mark.asyncio
async def test_codex_tool_argument_errors_respect_tool_data_redaction(
monkeypatch: pytest.MonkeyPatch,
input_json: str,
cause_type: type[Exception],
redact: bool,
mode: Literal["python", "json"],
) -> None:
monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", redact)
tool = codex_tool(
CodexToolOptions(
codex=cast(Codex, FakeCodex(CodexMockState())),
failure_error_function=None,
parameter_validation_mode=mode,
)
)
context = ToolContext(
Expand Down
Loading