Skip to content

Replay an OpenAI Agents SDK agent's completed calls when Airflow retries its task - #74770

Merged
kaxil merged 2 commits into
mainfrom
openai-agents-durable
Oct 17, 2026
Merged

kaxil merged 2 commits into
mainfrom
openai-agents-durable

Conversation

@kaxil

@kaxil kaxil commented Oct 14, 2026

Copy link
Copy Markdown
Member

When a task running an OpenAI Agents SDK agent fails and Airflow retries it, the agent starts again from the prompt: every model call is paid for again and every tool call happens again. run_durably() / run_durably_sync() take the same arguments as Runner.run / Runner.run_sync and record each model call and function tool call in the task's durable journal, the one AgentOperator(durable=True) uses. A retry replays every call that asks what the failed try asked, in order, and runs live from the first that differs. Once the run succeeds the recording is deleted.

@task(retries=3)
def research(question: str) -> str:
    agent = Agent(name="analyst", model=model, tools=AirflowTools(warehouse))
    return run_durably_sync(agent, question).final_output

From Airflow 3.3 the journal is kept in the task state store; before that, under [common.ai] durable_cache_path, as for AgentOperator(durable=True). Stacked on the tracing PR.

Design rationale

Model calls are intercepted by wrapping the model, not by run hooks. RunHooks and AgentHooks return None, so they can observe a call but not replace it, and RunState.to_json() captures a run only at an interruption, not mid-run. The SDK resolves a model from RunConfig.model, then the model_provider for names, then a Model on the agent, so run_durably wraps the provider and the run_config model and clones the agent graph (following handoffs) to wrap Model instances and function tools. This is the same interception point the SDK's Temporal integration uses.

A replayed call has to turn into the same next request as the live one did. Each request is fingerprinted from the model, instructions, conversation, settings, tools (with descriptions and strictness), output schema, handoffs and prompt, and four things keep that stable across a replay:

  • Recorded answers keep only the fields the response set (exclude_unset), and None fields are left out of the fingerprint. Otherwise replayed items carry explicit nulls the live ones do not, and every request after the first replay mismatches.
  • Tool results are recorded as the SDK sends them to the model: structured ToolOutput* content as it is, anything else as str(output). Recording a Pydantic model, tuple or datetime as JSON would replay as different text.
  • The prompt_cache_key the SDK generates once per run is left out of the settings.
  • A call the SDK's own model retry makes again reuses the failed call's position instead of taking a new one, so the next try still lines up.

An agent used through as_tool is one recorded call. Its own model calls run inside the outer tool call, so they run live rather than taking positions of the outer run; the outer call replays whole.

A run the model failed asks the model again. For MaxTurnsExceeded, ModelBehaviorError, ModelRefusalError and output or tool-input guardrail tripwires the recorded answers would replay into the same failure, so the journal reruns from the model's last request.

Server-side conversations are refused. previous_response_id, conversation_id and auto_previous_response_id keep the conversation on OpenAI's servers, which a replayed call never reaches, so run_durably raises ValueError for them.

Known issues

  • Not recorded, so they run live on every try: tools other than function tools (hosted tools, mcp_servers tools, shell and computer tools), a function tool whose needs_approval is a function (the SDK checks the approved arguments only through its own invoker, so wrapping it would weaken that check), an agent reached through handoff() unless its model is given by name, and streamed runs. A session makes every retry run live. The guide lists all of these.
  • A ModelBehaviorError that a tool raises, such as arguments failing validation with failure_error_function=None, counts as the tool's failure: the tool reruns, but the answer that asked for it replays.
  • Small follow-ups: passing result.last_agent back into run_durably wraps it twice; the wrapped model provider is not closed when run_config is omitted, which matters only for the 0.23 websocket transport; a dict run_config is not accepted.

  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@kaxil
kaxil added this pull request to stack #74771 October 14, 2026 22:05
@kaxil kaxil closed this Oct 14, 2026
@kaxil kaxil reopened this Oct 14, 2026
@kaxil
kaxil force-pushed the openai-agents-durable branch from ccb390c to 6a2eaea Compare October 15, 2026 12:38
@kaxil
kaxil force-pushed the openai-agents-durable branch 2 times, most recently from 76baf91 to 38ad01b Compare October 15, 2026 12:47

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

A few questions, mostly about test coverage and wording in the guide.


Drafted-by: Claude Code (Sonnet 5.5); reviewed by @pankajastro before posting

@kaxil
kaxil force-pushed the openai-agents-durable branch from 38ad01b to 4285715 Compare October 15, 2026 15:06
Base automatically changed from openai-agents-tracing to main October 15, 2026 17:48
…ies its task

run_durably() and run_durably_sync() run an agent like Runner.run, recording
each model call and function tool call in the task's durable journal, the one
AgentOperator(durable=True) uses. A retry replays every call that asks what the
failed try asked, in order, and runs live from the first that differs. A run
the model failed (max turns, malformed output, output guardrail) asks the
model again from its last request; an agent called through as_tool replays as
one call; server-side conversations are refused because a replay never
reaches them.
@kaxil
kaxil force-pushed the openai-agents-durable branch from 4285715 to 1002f07 Compare October 15, 2026 17:48
…ing its error

A function tool's exception that failure_error_function turned into text was
recorded as the tool's result, so a retry replayed the error and never ran the
tool again. Such a call is no longer recorded, as a call that raised is not.
The guide now splits what a retry replays from what always runs live, names
the journal's storage, and the tests cover unreadable recordings, structured
results, sessions, and a tool-raised ModelBehaviorError.
@kaxil
kaxil merged commit 0eab1db into main Oct 17, 2026
86 checks passed
@kaxil
kaxil deleted the openai-agents-durable branch October 17, 2026 12:55
SameerMesiah97 pushed a commit to SameerMesiah97/airflow-oss-sm97 that referenced this pull request Oct 27, 2026
…ies its task (apache#74770)

run_durably() and run_durably_sync() run an agent like Runner.run, recording
each model call and function tool call in the task's durable journal, the one
AgentOperator(durable=True) uses. A retry replays every call that asks what the
failed try asked, in order, and runs live from the first that differs. A run
the model failed (max turns, malformed output, output guardrail) asks the
model again from its last request; an agent called through as_tool replays as
one call; server-side conversations are refused because a replay never
reaches them.

A function tool's exception that failure_error_function turned into text was
recorded as the tool's result, so a retry replayed the error and never ran the
tool again. Such a call is no longer recorded, as a call that raised is not.
The guide now splits what a retry replays from what always runs live, names
the journal's storage, and the tests cover unreadable recordings, structured
results, sessions, and a tool-raised ModelBehaviorError.
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.

5 participants