Skip to content

Support resuming RunState inside workflows - #55

Merged
brianstrauch merged 3 commits into
temporalio:mainfrom
avanish-garg:fix/openai-agents-run-state-resume
Oct 14, 2026
Merged

brianstrauch merged 3 commits into
temporalio:mainfrom
avanish-garg:fix/openai-agents-run-state-resume

Conversation

@avanish-garg

Copy link
Copy Markdown
Contributor

What was changed

  • New temporalio.openai_agents.workflow.run_state_from_string(agent, state_string, ...): builds a
    RunState against Temporal-converted agents (outside a workflow, or without the Temporal runner, it is
    plain RunState.from_string). README section added under Tool Calling.
  • Runner.run / run_streamed raise a ValueError for a RunState whose current agent is not
    converted, pointing at the helper (a ValueError fails the workflow task, so a long-lived approval
    workflow is not lost and can be fixed by a deploy).
  • _convert_agent now keeps the _agent_ref of converted handoff() objects, so a state paused after
    a handoff can be resumed (it previously failed with "Run state agent not found in agent map").

Why?

Resuming a serialized RunState (approvals after continue-as-new) hung the workflow, see the issue.
The SDK matches approvals by agent identity, so the state has to be built against the converted
agents from the start.

Checklist

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

  2. How was this tested:
    tests/test_openai_run_state.py: resume from a string, resume after a handoff (plain agent and
    handoff() object), resume across a real workflow.continue_as_new carrying the string, the guard
    rejecting an unconverted state, and the helper outside a workflow. make lint is clean and
    make test passes (254); the new tests also pass on openai-agents 0.22.3 and 0.23.1.
    Not covered: run_streamed with an unconverted state (shares the same code path, no dedicated
    test), nested agent-as-tool states, reject(), sandbox agents.

  3. Any docs updates needed?
    README section included.

@CLAassistant

CLAassistant commented Oct 7, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@avanish-garg
avanish-garg marked this pull request as ready for review October 8, 2026 13:59
@avanish-garg
avanish-garg requested a review from a team as a code owner October 8, 2026 13:59

@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.

Here's a suggestion for addressing this bug in the runner so users can continue using the existing OpenAI Agents SDK API, rather than adding workflow.run_state_from_string and rejecting states restored with RunState.from_string:

I reproduced #53 on current main and verified a local prototype that supplies an internal Temporal ModelProvider through RunConfig.model_provider. Its get_model(model_name) returns a _TemporalModelStub, so the SDK can resume with the original agents stored in the state while model calls still execute as Temporal activities. This preserves the agent identities used to match approvals without rewriting private RunState structures. The provider belongs in the shared preparation path for both Runner.run and Runner.run_streamed; the new unconverted-state guard would need to be removed for supported string/None models.

The intended user code remains:

state = await RunState.from_string(agent, saved_state)
for interruption in state.get_interruptions():
    state.approve(interruption)
result = await Runner.run(agent, state)

One detail matters for compatibility: ModelProvider.get_model receives only the model name, while the existing stub uses the active agent for activity summaries and ModelSummaryProvider. In the prototype, I wrap the public RunConfig.call_model_input_filter callback to capture CallModelData.agent in a per-provider ContextVar, forwarding the user's existing filter and its result unchanged. The stub resolves that captured agent when constructing the activity input. This lookup needs to be deferred until the model is invoked because the streaming SDK resolves the model before it invokes the input filter. Keeping the capture task-local also preserves the correct summaries for concurrent nested agents.

@avanish-garg
avanish-garg force-pushed the fix/openai-agents-run-state-resume branch from 5eb6804 to 47b64fa Compare October 8, 2026 20:21
A RunState restored with RunState.from_string(agent, ...) holds the caller's
own agents, and the Agents SDK resumes a run with the agents stored in the
state rather than the converted copies the plugin passes to Runner.run. Their
string or None models were resolved by the default model provider inside the
workflow, which failed the workflow task repeatedly until the workflow timed
out.

Install an internal ModelProvider through RunConfig.model_provider for both
Runner.run and Runner.run_streamed. It returns the Temporal model stub for
string and None models, so model calls from resumed runs still execute as
activities and the original agents keep their identity for approval matching.

ModelProvider.get_model only receives the model name, so the stub finds the
agent for activity summaries and ModelSummaryProvider by wrapping the user's
call_model_input_filter: the wrapper records CallModelData.agent in a
ContextVar and forwards to the user's filter, returning its result unchanged.
The stub reads the agent when it builds the activity input, because streaming
resolves the model before the filter runs. The ContextVar keeps the captured
agent task-local for concurrent nested runs.
The model provider recorded the active agent in a ContextVar that was created
for every top-level run and never reset. The Agents SDK runs the first model
turn of a non-streaming run in a child task but later turns inline in the
caller's task, so each completed run left a context entry holding its agent in
a long-lived workflow task, and memory grew across repeated runs.

Use one module-level ContextVar and enter capture_scope() around the
non-streaming run. The scope sets the variable on entry and resets it with the
token on exit, including on errors and cancellation, which removes the entry
from the caller's context and restores the value seen by an enclosing run.
Streamed runs execute in a task with its own copy of the context, so nothing
is captured in the caller's context and they need no scope.

Add tests that run agents back to back in one workflow and assert that no
agent is reachable from the workflow's context afterwards, for successful,
failing, cancelled and streamed runs.
@avanish-garg
avanish-garg force-pushed the fix/openai-agents-run-state-resume branch from 47b64fa to 65ca85e Compare October 14, 2026 13:20

@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.

Thank you!

@brianstrauch
brianstrauch merged commit 33bc20b into temporalio:main Oct 14, 2026
15 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: a RunState rebuilt with RunState.from_string and resumed in a workflow hangs (approvals after continue-as-new)

3 participants