Skip to content

[Bug] openai_agents: a sync @function_tool never runs in a Workflow; the model gets an empty error #231

Closed

Description

@campoy

Package: temporalio-openai-agents 1.1.0 and main (c664a34); by source, also temporalio.contrib.openai_agents in temporalio 1.34.0.

What happens

The README's inline tool example, calculate_circle_area, is a sync @function_tool. In a Workflow, a sync function tool never runs:

  • The Agents SDK runs sync function tools with asyncio.to_thread, which calls the running loop's run_in_executor. The Workflow event loop doesn't implement it and raises NotImplementedError().
  • The SDK's default tool error handler turns that into the tool result An error occurred while running the tool. Please try again. Error: , with nothing after it, because the exception has no message.
  • The model gets that text instead of the result, and the Workflow completes. No Workflow Task fails and nothing is logged.

Related cases:

  • A sync tool with needs_approval=True pauses, but after approval its body still doesn't run.
  • A sync tool that writes the run context never writes it.
  • With failure_error_function=None, Runner.run raises UserError('Error running tool add: ') caused by NotImplementedError, and the Workflow Task fails and retries.
  • The same tool as async def works in a Workflow, and the sync tool works outside Temporal.

Reproduction

One file, no network: a scripted model calls the tool once, and the Workflow returns the tool output the model got back. It needs a local dev server (the temporal CLI on PATH, or start_local downloads one).

"""A sync @function_tool never runs in a Workflow: the model gets an empty error instead.

Uses a scripted model and a local dev server (the `temporal` CLI on PATH, or a download).
Exits 1 while the sync tool fails, 0 once it returns its result like the async control.
"""

import asyncio
import shutil
import sys
import uuid

from agents import Agent, Runner, function_tool
from temporalio import workflow
from temporalio.openai_agents import OpenAIAgentsPlugin
from temporalio.openai_agents.testing import ResponseBuilders, TestModel, TestModelProvider
from temporalio.testing import WorkflowEnvironment
from temporalio.worker import Worker


@function_tool
def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b


@function_tool(name_override="add")
async def add_async(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b


@workflow.defn
class CalculatorWorkflow:
    @workflow.run
    async def run(self, use_async: bool) -> str:
        agent = Agent(name="Calculator", tools=[add_async if use_async else add])
        result = await Runner.run(agent, "What is 2 + 3?")
        # The tool output the model got back.
        return repr(next(i.output for i in result.new_items if i.type == "tool_call_output_item"))


async def main() -> int:
    call_add = ResponseBuilders.tool_call('{"a": 2, "b": 3}', "add")
    done = ResponseBuilders.output_message("done")
    model = TestModel.returning_responses([call_add, done, call_add, done])
    plugin = OpenAIAgentsPlugin(model_provider=TestModelProvider(model))
    async with await WorkflowEnvironment.start_local(
        plugins=[plugin], dev_server_existing_path=shutil.which("temporal")
    ) as env:
        async with Worker(env.client, task_queue="tq", workflows=[CalculatorWorkflow]):
            outputs = {}
            for use_async in (False, True):
                outputs[use_async] = await env.client.execute_workflow(
                    CalculatorWorkflow.run, use_async, id=str(uuid.uuid4()), task_queue="tq"
                )
    print("sync tool output: ", outputs[False])
    print("async tool output:", outputs[True])
    return 0 if outputs[False] == outputs[True] == "5" else 1


if __name__ == "__main__":
    sys.exit(asyncio.run(main()))

On temporalio-openai-agents 1.1.0 from PyPI (temporalio 1.34.0, openai-agents 0.22.3), it exits 1:

sync tool output:  'An error occurred while running the tool. Please try again. Error: '
async tool output: 5

On main at c664a34 (openai-agents 0.22.0 from uv.lock) the output is the same, and it exits 1.

Expected

The sync tool runs in the Workflow and the model gets 5, as with the async tool and as outside Temporal.

Proposed fix

Run sync function tools directly in the Workflow, without changing the tools. During each Runner.run and Runner.run_streamed in a Workflow, the Workflow loop's run_in_executor runs a call inline only when it is the Agents SDK's asyncio.to_thread call for a sync @function_tool (called from agents.tool). The tool's own invoker still parses arguments, checks output and handles errors. Every other run_in_executor call fails as before, and async tools are unchanged.

Runs that already got the error and acted on it would replay differently, so the change needs a workflow.patched check. It asks only when a sync tool runs.

A PR follows.

Activity

  1. added
    bug
    on Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions