Repository navigation
Support resuming RunState inside workflows - #55
brianstrauch merged 3 commits into
Conversation
There was a problem hiding this comment.
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.
5eb6804 to
47b64fa
Compare
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.
47b64fa to
65ca85e
Compare
What was changed
temporalio.openai_agents.workflow.run_state_from_string(agent, state_string, ...): builds aRunStateagainst Temporal-converted agents (outside a workflow, or without the Temporal runner, it isplain
RunState.from_string). README section added under Tool Calling.Runner.run/run_streamedraise aValueErrorfor aRunStatewhose current agent is notconverted, pointing at the helper (a
ValueErrorfails the workflow task, so a long-lived approvalworkflow is not lost and can be fixed by a deploy).
_convert_agentnow keeps the_agent_refof convertedhandoff()objects, so a state paused aftera 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
Closes [Bug] openai_agents: a RunState rebuilt with RunState.from_string and resumed in a workflow hangs (approvals after continue-as-new) #53
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 realworkflow.continue_as_newcarrying the string, the guardrejecting an unconverted state, and the helper outside a workflow.
make lintis clean andmake testpasses (254); the new tests also pass on openai-agents 0.22.3 and 0.23.1.Not covered:
run_streamedwith an unconverted state (shares the same code path, no dedicatedtest), nested agent-as-tool states,
reject(), sandbox agents.Any docs updates needed?
README section included.