Skip to content

Return invalid tool arguments to the model instead of failing the workflow - #92

Merged
brianstrauch merged 3 commits into
temporalio:mainfrom
avanish-garg:fix/openai-agents-tool-argument-errors
Oct 19, 2026
Merged

brianstrauch merged 3 commits into
temporalio:mainfrom
avanish-garg:fix/openai-agents-tool-argument-errors

Conversation

@avanish-garg

@avanish-garg avanish-garg commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

  • New _parse_tool_arguments in workflow.py, mirroring the Agents SDK's function_tool parsing: bad JSON, a
    non-object payload and pydantic ValidationError become ModelBehaviorError with the same messages as a stock
    function tool.
  • run_activity (activity_as_tool) and run_operation (nexus_operation_as_tool) catch only that
    ModelBehaviorError, around the parse step alone, and return the formatted text as the tool output. The
    activity or operation is not run for bad arguments.
  • New parameter failure_error_function: ToolErrorFunction | None = default_tool_error_function formats the error text returned to the model. None opts out of model feedback and fails the workflow with a non-retryable ApplicationError (type ModelBehaviorError).

With failure_error_function=None, an invalid tool call fails the workflow promptly with a non-retryable ApplicationError of type ModelBehaviorError. The Agents runner surfaces it as an AgentsWorkflowError, the same shape as other tool failures. It does not retry the workflow task, and the model never sees the error.

  • Errors raised by the activity itself are unchanged (still fail the workflow with AgentsWorkflowError), and
    the successful path is unchanged.
  • An empty argument string is now treated as {}, as a stock function_tool does (a zero-parameter tool runs; a
    tool with required parameters gets the validation error). Before, it failed the workflow.

Why?

One malformed model call (missing/wrong-typed argument, JSON list, not JSON) either retried the workflow task
until the execution timeout or failed the whole workflow, where a normal @function_tool lets the model recover.
The conversion happens inside on_invoke_tool because the SDK's own error wrapper only applies to tools built by
function_tool, and it would also swallow activity errors, which must keep their current behaviour.

Checklist

  1. Closes [Bug] openai_agents: activity_as_tool / nexus_operation_as_tool fail or wedge the workflow when the model sends invalid tool arguments #91

  2. How was this tested:
    New tests/test_openai_tool_argument_errors.py (35 tests): eight bad-input shapes for activity_as_tool and
    five for nexus_operation_as_tool (the workflow completes and the model receives the error text; the
    activity/operation is not run), text equality with a stock function_tool in redacted and unredacted modes,
    custom and None failure_error_function, the parsing helper, and regressions (valid arguments in four shapes,
    an activity that raises still fails the workflow). On main, 20 of the 26 selected tests fail
    (Workflow timed out / AgentsWorkflowError); the 6 that pass are the regressions. make lint is clean and
    make test passes (311). Only openai-agents 0.22.0 (the pinned range) was checked.

  3. Any docs updates needed?
    The docstrings of both functions describe the new parameter. The README is not changed.

@avanish-garg
avanish-garg marked this pull request as ready for review October 15, 2026 09:03
@avanish-garg
avanish-garg requested a review from a team as a code owner October 15, 2026 09:03

@brianstrauch brianstrauch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two suggestions: make failure_error_function=None fail the workflow promptly, and add workflow-level coverage for corrected tool calls and async error callbacks.

@avanish-garg
avanish-garg force-pushed the fix/openai-agents-tool-argument-errors branch from f67e2d7 to 1fbc9af Compare October 16, 2026 09:08
@avanish-garg

Copy link
Copy Markdown
Contributor Author

Thanks @brianstrauch!
Done with the changes!

…kflow

activity_as_tool and nexus_operation_as_tool validated model-supplied
arguments inside the workflow without handling errors. A missing or
wrong-typed argument, or a JSON list, raised a pydantic ValidationError
that the Agents SDK wraps in a UserError. That is not a Temporal failure
exception, so the workflow task failed and was retried indefinitely.
Arguments that were not valid JSON raised an ApplicationError that failed
the whole workflow.

Parse and validate the arguments the way function_tool does and return the
error text to the model as the tool output so it can retry. Errors raised
by the activity or Nexus operation itself are unchanged. An empty input
string is now treated as no arguments, as in function_tool.

Add a failure_error_function parameter to both functions to customize the
error text. It defaults to the Agents SDK's default_tool_error_function and
raises the error if None.
…e model

With failure_error_function=None, activity_as_tool and nexus_operation_as_tool
re-raised ModelBehaviorError. The Agents SDK passes that through unwrapped, and
the plugin does not treat it as a workflow failure, so the workflow task was
retried until the execution timed out. Raise a non-retryable ApplicationError of
type ModelBehaviorError instead, which the Agents runner surfaces as an
AgentsWorkflowError and fails the workflow.

Add workflow-level tests for both tools covering a corrected call after an
invalid one, failure_error_function=None, and an async failure_error_function.
With failure_error_function=None, an invalid tool call fails with a
non-retryable ApplicationError, but the runner rewrapped it as an
AgentsWorkflowError, which serializes as retryable. A Workflow retry policy
therefore reran the whole agent run for a failure marked non-retryable.

When the cause is a non-retryable ApplicationError, the runner now raises a
subclass of AgentsWorkflowError that serializes as non-retryable. The failure
type and message are unchanged, and all other failures keep their retryability.
@avanish-garg
avanish-garg force-pushed the fix/openai-agents-tool-argument-errors branch from 1fbc9af to 15520c1 Compare October 19, 2026 09:19
@brianstrauch
brianstrauch merged commit 80a48fa into temporalio:main Oct 19, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] openai_agents: activity_as_tool / nexus_operation_as_tool fail or wedge the workflow when the model sends invalid tool arguments

2 participants