Skip to content

[Bug] openai_agents: a RunState rebuilt with RunState.from_string and resumed in a workflow hangs (approvals after continue-as-new) #53

Closed

Description

@avanish-garg

What are you really trying to do?
Human-in-the-loop with @function_tool(needs_approval=True): persist the paused RunState as a
string (for example to pass through continue_as_new when history gets long), approve later, resume.

Describe the bug
Resuming works in memory, but after state.to_string() / RunState.from_string(agent, s) the run
uses the original (unconverted) agent objects, so the real OpenAI model provider runs inside the
workflow and raises RestrictedWorkflowAccessError (platform.system). That fails the workflow
task, and the workflow hangs until its timeout. The openai-agents SDK matches approvals by agent object
identity, so just swapping _current_agent / _starting_agent in the runner stops the network access
but the approval is then not honored (the state's items still hold the original agent objects).

Minimal Reproduction

@function_tool(needs_approval=True)
async def dangerous(x: int) -> str:
    "do something dangerous"
    return f"done {x}"

@workflow.defn
class WF:
    @workflow.run
    async def run(self, mode: str) -> str:
        agent = Agent(name="a", instructions="x", tools=[dangerous])
        r = await Runner.run(agent, "go")
        state = r.to_state()
        if mode != "direct":
            state = await RunState.from_string(agent, state.to_string())
        for i in state.get_interruptions():
            state.approve(i)
        r2 = await Runner.run(agent, state)
        return str(r2.final_output)

mode="direct" completes. Any other mode times out with RestrictedWorkflowAccessError in the worker
log. (Test model: a TestModel that returns a tool call to dangerous, then a message.)
Building the state with the private _convert_agent(...) result instead works.

Environment/Versions
macOS arm64, Python 3.11, temporalio 1.34.0, openai-agents 0.22.3 and 0.23.1.

Additional context
I put a proposed fix on a branch and will open it as a draft PR so it is easy to react to. It adds
temporalio.openai_agents.workflow.run_state_from_string(agent, state_string, ...), which builds the
state against Temporal-converted agents, plus a guard that makes Runner.run/run_streamed raise a
clear ValueError (pointing at the helper) for a state whose current agent is not converted. I'm
happy to change the shape (public helper vs. making the runner rebind agents inside the state) if you
prefer a different approach; I chose the helper because rebinding would mean rewriting several private
structures of RunState.

Activity

  1. added
    bug
    on Oct 7, 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