Repository navigation
Breaking: openai_agents: run your agents, not copies, so permanent approvals carry over - #426
Conversation
The Workflow runner no longer copies the agent graph to put a Temporal model stub in each agent. The plugin's RunConfig.model_provider already turns every model name into the model Activity, so the SDK now runs the caller's agents as they are. This also drops the fix-ups of the private FunctionTool._agent_instance and Handoff._agent_ref fields. From openai-agents 0.23, a permanent approval (always_approve=True) belongs to the agent that owns the tool. With a new copy on each run, a later run with the same agent and context asked again. Without copies the approval stays on the caller's agent, as it does without the plugin. The input filter the plugin installs now refuses an agent whose model is a Model object just before that agent calls its model, instead of when the run starts. The tests that called the removed copy directly go: with no copy they would only test the SDK.
|
|
||
| kwargs["run_config"] = run_config | ||
| return _convert_agent(self.model_params, starting_agent, None), run_config | ||
| return run_config |
There was a problem hiding this comment.
| return run_config | |
| # Refuse a Model object up front for every agent reachable through public | |
| # fields. Agents behind handoff(), as_tool() or a custom on_invoke_handoff are | |
| # only reachable through private SDK fields, so the call_model_input_filter | |
| # refuses them just before their model call instead. | |
| pending: list[Agent[Any]] = [starting_agent] | |
| checked: set[int] = set() | |
| while pending: | |
| agent = pending.pop() | |
| if id(agent) in checked: | |
| continue | |
| checked.add(id(agent)) | |
| if agent.model is not None and not isinstance(agent.model, str): | |
| raise ValueError( | |
| "Temporal workflows require a model name to be a string in the agent." | |
| ) | |
| pending.extend(h for h in agent.handoffs if isinstance(h, Agent)) | |
| return run_config |
Can we keep the early refusal where public fields allow it? The copy used to refuse an agent whose model is a Model object when the run started; this PR moves that to the moment the agent calls its model. A read-only check in _prepare_workflow_run can get most of the early failure back without touching private fields:
starting_agent.model- every
Agentreachable throughagent.handoffs, recursively (track visited agents, since handoff graphs can loop)
Both are public, and they cover the usual setups: one agent, or agents that hand off to each other by listing them in handoffs.
For the rest, the filter's late check is the backstop, and I think that's acceptable as long as we say so: handoff(agent) keeps its target in Handoff._agent_ref, as_tool() keeps its agent in FunctionTool._agent_instance, and a custom on_invoke_handoff can return any agent. Reaching the first two would bring back the private-field dependency this PR removes, and the third is only known at handoff. Could the filter's docstring (or a comment by the early check) note that agents behind handoff(), as_tool() and custom on_invoke_handoff are checked late on purpose, to avoid depending on private SDK fields?
For as_tool(), the nested run goes through _prepare_workflow_run too, so the starting-agent check refuses it before the nested model call. The default failure_error_function still returns that error to the parent model as the tool's output. That matches upstream, but it would be worth a line in the breaking-change notes.
There was a problem hiding this comment.
Done. The runner now refuses a Model object when the run starts, for the starting agent and every Agent in handoffs, with a visited set for loops. I put the check at the top of _prepare_workflow_run, next to the other agent checks, so it fails before any setup. It also lets _TemporalModelStub through, because a nested run on the copy path (see your patch comment) gets a copy with a stub.
The comment by the check says why agents behind handoff(), as_tool() and a custom on_invoke_handoff are checked late. The breaking notes now say an as_tool() agent fails when its nested run starts, and the error goes back to the parent model as the tool's output.
| # A Model object would run in the Workflow, so it is refused before the call. | ||
| agent.model = ScriptedModel(_banker_reply) | ||
| with pytest.raises(ValueError, match="model name to be a string"): | ||
| no_filter.call_model_input_filter(_call_data(agent)) |
There was a problem hiding this comment.
| no_filter.call_model_input_filter(_call_data(agent)) | |
| no_filter.call_model_input_filter(_call_data(agent)) | |
| def test_prepare_workflow_run_refuses_model_objects_reachable_through_handoffs(): | |
| target = Agent(name="target", model=ScriptedModel(_banker_reply)) | |
| middle = Agent(name="middle", handoffs=[target]) | |
| starting = Agent(name="starting", handoffs=[middle]) | |
| middle.handoffs.append(starting) # a loop must not recurse forever | |
| runner = TemporalOpenAIRunner(ModelActivityParameters()) | |
| with pytest.raises(ValueError, match="model name to be a string"): | |
| runner._prepare_workflow_run(starting, {}) |
Test that we can reject asap for invalid model
There was a problem hiding this comment.
Added as you wrote it, right after the filter test.
| ) | ||
|
|
||
| kwargs["run_config"] = run_config | ||
| return _convert_agent(self.model_params, starting_agent, None), run_config |
There was a problem hiding this comment.
The replay section of the PR desc says runs that depend on agent-mutating hooks, or that use one SandboxAgent in two runs at once, can fail with [TMPRL1100], and suggests draining or Worker Versioning. Could we gate the change behind a patch instead, as _prepare_workflow_run already does for openai_agents.refuse-run-model-provider? workflow.patched() is memoized per workflow, so if workflow.patched("openai_agents.run-caller-agents"): return starting_agent, run_config else _convert_agent(...) keeps workflows started before 2.0 on copies for their whole lifetime, and new workflows run the caller's agents. That also keeps in-flight runs on their current model settings, which otherwise change mid-conversation without a replay failure. _convert_agent would stay until the patch is deprecated and removed in a later release.
One catch: on the copy path every agent's model is a _TemporalModelStub, so the new call_model_input_filter check needs to let _TemporalModelStub through, or it refuses the workflows the patch protects.
A major version tells users the API changed, but upgrading still replays in-flight workflows on the new code, and many users of this plugin have long-running approval workflows they can't easily drain.
There was a problem hiding this comment.
Good call, done. openai_agents is GA, so open runs have to keep replaying.
- A new run records
openai_agents.run-caller-agentsand runs your agents. A run that already ran an agent before the upgrade stays on copies until it ends, so_convert_agentand its tests are back. - The filter lets
_TemporalModelStubthrough, as you pointed out. - Two new replay histories for a small workflow whose next command depends on getting its own agent back: one recorded on
mainand one recorded here with the marker. Both replay. With the gate turned off, themainone fails with[TMPRL1100], so the test shows the gate is doing the work.
I rewrote the replay section of the description to match.
openai_agents is GA, so runs that are already open must replay. A run now runs the caller's agents only once it records the openai_agents.run-caller-agents patch marker. Runs started without it keep running on copies with model stubs until they end, so _convert_agent and its tests come back for them. The input filter lets those stubs through. The runner also refuses a Model object when the run starts again, for the starting agent and every agent in handoffs. Agents behind handoff(), as_tool() or a custom on_invoke_handoff are only reachable through private SDK fields, so the filter still refuses them just before their model call. Two new replay histories cover the gate: one recorded on main, which runs copies, and one recorded here with the marker. Without the gate, the main history fails with [TMPRL1100].
The squash of the RunConfig.model change dropped the import, which the copy path for older runs still uses.
|
|
||
| kwargs["run_config"] = run_config | ||
| return _convert_agent(self.model_params, starting_agent, None), run_config | ||
| return run_config |
There was a problem hiding this comment.
Done. The runner now refuses a Model object when the run starts, for the starting agent and every Agent in handoffs, with a visited set for loops. I put the check at the top of _prepare_workflow_run, next to the other agent checks, so it fails before any setup. It also lets _TemporalModelStub through, because a nested run on the copy path (see your patch comment) gets a copy with a stub.
The comment by the check says why agents behind handoff(), as_tool() and a custom on_invoke_handoff are checked late. The breaking notes now say an as_tool() agent fails when its nested run starts, and the error goes back to the parent model as the tool's output.
| # A Model object would run in the Workflow, so it is refused before the call. | ||
| agent.model = ScriptedModel(_banker_reply) | ||
| with pytest.raises(ValueError, match="model name to be a string"): | ||
| no_filter.call_model_input_filter(_call_data(agent)) |
There was a problem hiding this comment.
Added as you wrote it, right after the filter test.
| ) | ||
|
|
||
| kwargs["run_config"] = run_config | ||
| return _convert_agent(self.model_params, starting_agent, None), run_config |
There was a problem hiding this comment.
Good call, done. openai_agents is GA, so open runs have to keep replaying.
- A new run records
openai_agents.run-caller-agentsand runs your agents. A run that already ran an agent before the upgrade stays on copies until it ends, so_convert_agentand its tests are back. - The filter lets
_TemporalModelStubthrough, as you pointed out. - Two new replay histories for a small workflow whose next command depends on getting its own agent back: one recorded on
mainand one recorded here with the marker. Both replay. With the gate turned off, themainone fails with[TMPRL1100], so the test shows the gate is doing the work.
I rewrote the replay section of the description to match.
#565) ## What was changed Replay is promised only within a major version now. A history recorded on a 1.x release must replay on every later 1.x release, and nothing is promised from 1.x to 2.x. So why does CI still replay 1.x histories on code that's heading to 2.0? It shouldn't, and this PR stops it. - Each plugin's committed histories move into a folder per major, `histories/<major>.x/`. - Each replay test gets a `REPLAY_MAJOR` constant (`ReplayMajor` in C#) and loads only that folder. Older majors' folders stay in the repo, but they don't replay. - AGENTS.md gets the rule, next to the one about gating history changes. A plugin's current major is the major of its next release. Here's what I used: | Plugin | Latest release | Current major | Replays | Kept, not replayed | | --- | --- | --- | --- | --- | | `python/openai_agents` | 1.1.0, then `Breaking:` commits | 2 | 4 histories in `2.x/` | 12 in `1.x/` | | `typescript/openai-agents` | 1.24.0 on npm (published from the SDK repo), then `Breaking:` commits | 2 | 1 in `2.x/` | 4 in `1.x/` | | `typescript/vercel-ai-sdk` | 1.0.0, then `Breaking:` commits | 2 | none | 3 in `1.x/` | | `python/google_genai` | 1.0.0rc1 | 1 | 1 in `1.x/` | 1 in `0.x/` | | `typescript/strands-agents` | none from this repo (npm has 1.21.0 to 1.24.0 from the SDK repo); 2.0.0 next | 2 | 4 in `2.x/` | 1 in `1.x/` | | `typescript/google-adk` | 0.1.0; 1.0.0 next | 1 | 2 re-recorded in `1.x/` | 2 in `0.x/` | | `python/google_adk` | 0.0.1; 1.0.0 next | 1 | none | 7 in `0.x/` | | `python/strands_agents` | 0.1.0; 1.0.0 next | 1 | 4 in `1.x/` | 5 in `0.x/` | | `python/deepagents` | 0.0.3; 1.0.0 next | 1 | none | 3 in `0.x/` | | `java/google-adk`, `dotnet/microsoft-agent-framework`, `python/microsoft_agent_framework` | below 1.0, not 1.0 next | 0 | all, in `0.x/` | none | A history recorded on `main` before a breaking fix goes with the release whose behavior it recorded. So the five OpenAI Agents Python histories from before #204, #373 and #426, and the four TypeScript ones from before #373, are 1.x histories. Strands Python's four MCP histories were recorded after #190 moved its MCP Activities to `temporalio-mcp`, code that 0.1.0 never had, so they're 1.x histories too. Strands TypeScript's `hello-agent-before-model-config` was recorded before #158, so it has 1.24.0's behavior and is a 1.x history. Its other four were recorded after #158, so they're 2.x. Tests that replayed only older-major histories go: Vercel AI SDK's three committed-history tests, Google GenAI's `legacy_ascii_files` replay, Google ADK Python's two replay tests with their workflows, Strands Python's `test_legacy_history_replay.py` and `test_model_context_overflow_legacy_replay.py`, Deep Agents' `test_legacy_cache_replay.py`, and Strands TypeScript's test that its history from before #158 fails to replay. The OpenAI Agents lists keep only the current major's files. Google ADK TypeScript's replay test checks a live run against a checked-in fixture, so I recorded its two fixtures again into `1.x/` with its own `UPDATE_ADK_HISTORIES=1`. `go/googleadk` doesn't change: it has no committed histories, and its replayer tests replay runs the same test just recorded. ## Why? We decided that replay is promised only within a major. But CI still replayed 1.x histories, so a fix for 2.0 had to keep 1.x runs replaying behind a patch, even though we don't promise that. With this PR, the next one can remove those patches from OpenAI Agents Python before 2.0.0 ships. ## Checklist 1. No issue. 2. How was this tested: - Python, for each of the six plugins: `make sync`, `make format`, `make lint`, and the tests that read committed histories (3, 7, 43, 11, 4 and 42 passed). - TypeScript, for each of the four plugins, in Docker with Node 24: `make sync`, `make lint`, and the test files that read committed histories. - `check_conventions.py` passes, and `detect_changes.py` selects the 12 plugins. - After moving the 1.0-next plugins to major 1: Google ADK Python `make format`, `make lint` and `make test` (321 passed, 6 skipped); Google ADK and Strands TypeScript `make lint` and their replay tests, in Docker with Node 24. - After moving Strands Python and Deep Agents to major 1 and merging `main`: `make lint` and `make test` for both (277 and 261 passed). - After moving Strands TypeScript to major 2: `make lint` and its two replay test files, in Docker with Node 24 (8 passed). - Java and .NET didn't run locally (no JDK or .NET SDK on this machine). CI runs them. 3. Any docs updates needed? AGENTS.md has the rule. The Microsoft Agent Framework Python histories README says where recordings go now.
Replay is promised only within a major, so 2.0 doesn't need the three patches that only kept runs from before their fixes replaying: openai_agents.refuse-run-model-provider (#204), openai-agents-stateful-mcp-session-watch (#373) and openai_agents.run-caller-agents (#426). Every run now takes the new path: RunConfig.model_provider is refused, stateful MCP operations without a timeout watch their session, and runs use your agents, not copies. _convert_agent and the tests that only covered the agent copies go too. The three 2.x histories that had patch markers are recorded again without them, since a marker with no matching patched() call fails replay.
Replay is promised only within a major, so 2.0 doesn't need the three patches that only kept runs from before their fixes replaying: openai_agents.refuse-run-model-provider (#204), openai-agents-stateful-mcp-session-watch (#373) and openai_agents.run-caller-agents (#426). Every run now takes the new path: RunConfig.model_provider is refused, stateful MCP operations without a timeout watch their session, and runs use your agents, not copies. _convert_agent and the tests that only covered the agent copies go too. The three 2.x histories that had patch markers are recorded again without them, since a marker with no matching patched() call fails replay.
…replaying (#566) ## What was changed Three patches on `main` exist only to keep runs from before their fixes replaying. No release wrote their markers: PyPI has 1.0.0 and 1.1.0, and both shipped before these fixes. With replay promised only within a major (#565), 2.0.0 doesn't need them. So let's remove them before it ships: - `openai_agents.refuse-run-model-provider` (#204): `Runner.run` always refuses `RunConfig.model_provider`. - `openai-agents-stateful-mcp-session-watch` (#373): a stateful MCP operation with neither a schedule-to-start nor a schedule-to-close timeout always watches its session. - `openai_agents.run-caller-agents` (#426): runs always use your agents, not copies. `_convert_agent` goes, and so do `test_model_conversion_loops` and the four run-state tests that called it directly. The three 2.x histories that had markers are recorded again without them, as `caller-agent-workflow-history.json`, `stateful-mcp-cancel-wait-workflow-history.json` and `stateful-mcp-gather-wait-workflow-history.json`. Why? A history with a marker and no matching `patched()` call fails replay with `[TMPRL1100] Non-deprecated patch marker encountered`. The Python README drops the marker notes and says which 1.x runs don't replay. The TypeScript README keeps its note on `openai-agents-stateful-mcp-session-watch`: TypeScript has its own copy of that gate, and this PR doesn't touch it. This also takes `workflow.patched()` out of every `Runner.run`. That call is what exposed the sdk-core bug where a run whose first Update was rejected fails its own replay. ## What will you notice? (Breaking) These 1.x runs don't replay on 2.0. Let them finish on 1.x Workers before you upgrade, or keep them there with Worker Versioning. For the release notes: - A run that passed its own `RunConfig.model_provider` (#204). On 1.x it ran on the Worker's provider. On 2.0 its replay fails with the `ValueError` that refuses the provider. - A run in which a stateful MCP operation with `cancellation_type=WAIT_CANCELLATION_COMPLETED` and neither timeout finished in the same Workflow Task as another Activity (#373). On 2.0 the operation resolves one step later, so the run schedules its next Activities in another order (`[TMPRL1100]`). - A run whose next step depended on getting its own agents back, not copies, such as asking again for a permanent approval (#426) (`[TMPRL1100]`). A run on a `main` build that reached one of these gates recorded its marker, and it doesn't replay either. No release wrote those markers. Rolling back to 1.x isn't promised either: histories of 2.0 runs that hit the last two cases fail on 1.1.0 with `[TMPRL1100]`. ## Why? We decided that replay is promised only within a major, and that a gate that only protects an older major's runs is removed before the next major ships. These three only protect runs from before their fixes, so they go before 2.0.0. ## Checklist 1. No issue. 2. How was this tested: - `make format`, `make lint` and `make test` for `python/openai_agents` on Python 3.12: 618 passed, 2 skipped. - Replay, across builds. I recorded finished runs on Workers with no Workflow cache (a hello agent, `CallerAgentWorkflow`, and the four stateful MCP replay shapes, with and without `WAIT_CANCELLATION_COMPLETED`), plus a `CallerAgentWorkflow` run still waiting on its model Activity. I recorded them on `main`, on this PR, and the stateful MCP ones on 1.1.0 too. Then I replayed each of them, and the committed histories, on every build. - On this PR, only these fail: - the three 1.x cases above: the committed 1.x `caller-agent` (`[TMPRL1100]`), `run-model-provider` (the plugin's `ValueError`) and `stateful-mcp-gather-wait` (`[TMPRL1100]`) histories, and the `WAIT_CANCELLATION_COMPLETED` gather recorded on 1.1.0 (`[TMPRL1100]`); - every history with a marker: the three committed on `main` and all seven recorded on `main` (every `main` agent run records `openai_agents.run-caller-agents`), each with `[TMPRL1100] Non-deprecated patch marker encountered`. The other nine committed 1.x histories replay, and so do the 1.1.0 cancel runs, with and without `WAIT_CANCELLATION_COMPLETED`, and the 1.1.0 gather run without it. Everything recorded on this PR replays on it. - Rollback: this PR's histories on `main` and on 1.1.0. The caller-agent and `WAIT_CANCELLATION_COMPLETED` gather histories fail with `[TMPRL1100]`; the rest replay, including the waiting run. I didn't carry a live run from one build to another. - Control: a history with a renamed Activity fails with `[TMPRL1100]` on every build. 3. Any docs updates needed? The README changes are here. The 2.0.0 release notes should list the replay cases above.
What was changed
What if the plugin ran your agents, and not copies of them? That's this PR. It also means a permanent approval sticks across runs again on openai-agents 0.23 and 0.24.
This PR is stacked on #422, so the diff here shows only this PR's changes.
TemporalOpenAIRunnerruns your agents as you wrote them. It no longer copies the agent graph to put a Temporal model stub in each agent. Why was the copy there? To swap in Temporal's model. But the plugin'sRunConfig.model_provideralready turns every model name into the model Activity, so nothing else is needed.RunConfig.modelstays a name too. The same provider turns it into the model Activity, so the SDK applies that model's default settings, as it does without the plugin.SandboxAgent's capabilities add, such as the compaction threshold.openai_agents.run-caller-agentspatch marker, so_convert_agentstays for the older runs. openai_agents is GA, so open runs must keep replaying.modelis aModelobject when the run starts, for the starting agent and every agent in itshandoffs, recursively. Agents behindhandoff(),as_tool()or a customon_invoke_handoffare only reachable through private SDK fields (Handoff._agent_ref,FunctionTool._agent_instance), or only known at handoff. So thecall_model_input_filterthe plugin installs refuses them just before their model call instead. That keeps new runs off private fields.Why?
From openai-agents 0.23, a permanent approval,
approve(..., always_approve=True), belongs to the agent that owns the tool (openai/openai-agents-python#5144). The plugin made a new copy of the agent on every run, so a later run with the same agent and context asked for approval again. #422 supports 0.23 and 0.24 but leaves this open. Without copies, the approval stays on your agent, as it does without the plugin. The TypeScript plugin made the same move in #270.What will you notice? (Breaking)
RunResult.last_agentand the agents in aRunStateare yours.agent.modelthere is the name you set, not a stub. So careful: anything a hook or callback changes on an agent changes your agent.handoff(), or one a customon_invoke_handoffreturns, whosemodelis aModelobject fails when it's about to call its model, not when the run starts. An agent behindas_tool()fails when its nested run starts. In bothas_tool()cases, the error goes back to the parent model as the tool's output, as upstream does for any failed nested run.RunConfig.modelset, an agent left at its default model settings now sends the run model's defaults, as upstream: none forgpt-4o,reasoning.effort: mediumandverbosity: lowforgpt-5.4-pro. Before, it sent the defaults of its own model, or of the default model.SandboxAgentwhoseCompactioncapability sets no policy now gets the compaction threshold for its model, as upstream: the run's model, else the agent's, else the default model. Before, the stub hid the name, so the fixed 240,000 applied, and an agent with no model sent no threshold at all. An explicitCompaction(policy=...)on an agent with no model applies again; since openai_agents: align implicit model settings with the model a custom provider resolves #178 it was dropped.SandboxAgentobject can't serve two runs at the same time: the second run fails with upstream'sSandboxAgent '...' cannot be reused concurrently across runs. Before, each run had its own copy.All of this applies to runs that run their first agent after the upgrade. A run that already ran an agent keeps its copies and all of its old behavior until it ends, including asking again for a permanent approval.
Model calls keep the same Activity type. With
RunConfig.modelset, their summary is now the agent's name, as for other runs, where it was empty.What about replay? Open runs replay. Replay doesn't compare Activity inputs or summaries, so a run that now sends different model settings replays and continues with them. A run that ran an agent before the upgrade has no patch marker, and Python keeps the first patch decision for the whole run, so it stays on copies. A run that hadn't run an agent yet records the marker at its first agent run. Once a run records the marker, the code before this change can't replay it (
Non-deprecated patch marker encountered), so a rollback fails for those runs. Let them finish before rolling back, or use Worker Versioning.Checklist
Fixes [Feature Request] openai_agents: support openai-agents 0.23 and 0.24 #416. Depends on openai_agents: support openai-agents 0.23 and 0.24 #422.
How was this tested:
test_always_approve_covers_later_runs_of_the_same_agent(plain,Agenthandoff andhandoff(); streamed and not) extendsResumeRunStateWorkflow. It approves withalways_approve=True, resumes, then runs the same agent again with the same context and new tool call IDs. Does it catch the bug? Against openai_agents: support openai-agents 0.23 and 0.24 #422's code, which still copies agents, all 6 cases fail on 0.23.1 and 0.24.0 (the second run stops for approval). On 0.22.0 they pass either way, because 0.22 doesn't tie approvals to an agent.test_capture_filter_returns_user_result_unchangedalso checks that the filter refuses aModelobject.test_prepare_workflow_run_refuses_model_objects_reachable_through_handoffschecks the early refusal through a handoff loop.CallerAgentWorkflow, whose next command depends on getting its own agent back: one recorded onmain(copies, no timer) and one recorded here (marker, timer). Both replay. Does the gate matter? With the gate turned off, themainhistory fails with[TMPRL1100] Nondeterminism error: Timer machine does not handle this event.test_run_model_gets_its_own_default_settings(agpt-5.4-prorun model on an agent with no model) andtest_alignment_keeps_extra_args, from openai_agents: keep RunConfig.model as a name so the run model's defaults apply #458.gpt-5.6-solexample with and withoutOPENAI_DEFAULT_MODEL, and aSandboxAgentwith no model, withgpt-4o, with an explicit policy, and with a run model.test_model_conversion_loopsand the four run-state tests that call_convert_agentdirectly stay, since older runs still use it.make sync,make lintandmake test(Python 3.13) pass with openai-agents 0.22.0, 0.22.1, 0.22.2, 0.22.3, 0.23.1 and 0.24.0, each locked in a scratch copy (0.23.0 was never on PyPI).make build,check_wheel.pyandcheck_conventions.pypass.make lintandmake testpass with the locked 0.24.0 (561 passed, 1 skipped). CI runs the locked 0.24.0.