Skip to content

BUG: OpenAIResponseTarget fails the send when tool arguments don't match a FunctionTool's signature #3668

Closed

Description

Describe the bug

With fail_on_missing_function=False, OpenAIResponseTarget._execute_call_section_async turns bad tool arguments into a malformed_arguments function output, so the model can fix its call and the tool loop continues. That covers arguments that are not valid JSON ({not-json) and arguments that are not a JSON object (null, []). #3519 extended the second check to custom_functions.

Arguments that are a JSON object but don't match a FunctionTool's signature are not covered. FunctionTool.execute_async validates them against the tool's Pydantic argument model, and the ValidationError for a wrong type, a missing required argument or an unexpected key (the argument model forbids extra fields) propagates out of send_prompt_async. The function itself is never called.

Through PromptNormalizer, the request is stored with an error response and the exception is re-raised, so the model's function_call is dropped and the turn is lost. FunctionTool advertises strict: false, so nothing upstream guarantees that the arguments match the schema.

Steps/Code to Reproduce

The two model replies are stubbed, so no endpoint is needed.

import asyncio
import json
from unittest.mock import AsyncMock, patch

from pyrit.models import Message, MessagePiece
from pyrit.prompt_target import OpenAIResponseTarget, tool
from pyrit.setup import IN_MEMORY, initialize_pyrit_async


@tool
async def add(x: int, y: int = 1) -> int:
    """Add two numbers."""
    return x + y


async def main() -> None:
    await initialize_pyrit_async(memory_db_type=IN_MEMORY, load_defaults=False, silent=True)
    target = OpenAIResponseTarget(
        model_name="gpt-4o",
        endpoint="https://example.invalid/openai/v1",
        api_key="x",
        tools=[add],
    )
    call = MessagePiece(
        role="assistant",
        original_value=json.dumps(
            {"type": "function_call", "call_id": "call-1", "name": "add", "arguments": '{"x": "two"}'}
        ),
        original_value_data_type="function_call",
    ).to_message()
    done = Message.from_prompt(prompt="Done.", role="assistant")
    # The model replies are stubbed: the bad call, then the answer it would give after a tool error.
    with patch.object(target, "_handle_openai_request_async", new_callable=AsyncMock, side_effect=[call, done]):
        await target.send_prompt_async(message=Message.from_prompt(prompt="Add two and three", role="user"))


asyncio.run(main())

{"y": 3} (missing x) and {"x": 2, "unexpected": true} fail the same way. With "arguments": "null" the same script completes and the model gets the malformed_arguments output.

Expected Results

The model receives a malformed_arguments function output, as it does for non-object arguments. The function is not called and the loop continues with the next request. With fail_on_missing_function=True the send should still raise, which it already does (ValidationError is a ValueError).

Actual Results

After the first model request:

pydantic_core._pydantic_core.ValidationError: 1 validation error for addArguments
x
  Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='two', input_type=str]

raised from FunctionTool.execute_async (pyrit/prompt_target/common/tool_provider.py), called by _execute_call_section_async (pyrit/prompt_target/openai/openai_response_target.py).

Proposed fix

Validate before the tool runs, so a rejected call can't be confused with a failure inside the function. Add a validate_arguments method to Tool that does nothing by default (MCP servers already report input errors in the tool result), implement it in FunctionTool with the argument model, and call it from _execute_call_section_async. On a ValueError, return malformed_arguments with invoked=False, or re-raise when fail_on_missing_function=True. Catching ValidationError around execute_async instead would also catch errors raised inside the function after it ran, and record them as not invoked. Keeping the malformed_arguments shape means MessageToolCallScorer still counts these calls as not executed.

Versions

Windows 11, Python 3.14.7, PyRIT 1.2.0.dev0 from main (0a78eca)

I'd be happy to open a PR.

Activity

  1. its-amann commented on Oct 30, 2026

    @its-amann
    Contributor

    Hi! I'd like to work on this. Richard Lundeen (@richlundeen), could you assign it to me? I'll follow up with a PR that includes a fix and tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions