Skip to content

Use the framework's default OpenAI client or key in the default model provider (breaking) - #168

Merged
campoy merged 5 commits into
mainfrom
fix/openai-agents-python-native-provider-settings-ignored
Oct 28, 2026
Merged

campoy merged 5 commits into
mainfrom
fix/openai-agents-python-native-provider-settings-ignored

Conversation

@campoy

@campoy campoy commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

  • Without model_provider, the model Activity's default OpenAIProvider now uses the framework's defaults, as native runs do: the client set with set_default_openai_client, as it is, or else AsyncOpenAI(api_key=<key set with set_default_openai_key>, max_retries=0). With neither set, that client still reads OPENAI_API_KEY and OPENAI_BASE_URL, as before.
  • A client the application set keeps its own settings, retries included. Copying it with with_options(max_retries=0) would build a new client that reads OPENAI_CUSTOM_HEADERS, OPENAI_ORG_ID and OPENAI_PROJECT_ID again, so its headers could differ from the client the application set. The README says to give that client max_retries=0 to leave retries to Temporal.
  • The default is read when the Worker is built, as the old client was. The README's "Model Providers" section says so.
  • test_chat_completions_model gains default_client and default_key cases.

Why?

The OpenAI Agents SDK documents set_default_openai_client and set_default_openai_key to point every model call at another OpenAI-compatible endpoint or key. With OpenAIAgentsPlugin() and no model_provider, the plugin ignored both (#167): model calls, with their prompts, went to the Worker environment's endpoint with the environment's key, the run completed, and nothing warned. Without OPENAI_API_KEY the Worker could not be built, even when a default client was set.

Breaking change

A Worker that sets a framework default client or key and uses the plugin's default provider now sends its model calls to that client or key. To keep the environment's endpoint and key, pass model_provider=OpenAIProvider(openai_client=AsyncOpenAI(max_retries=0)) with a model Activity timeout (a custom provider requires one).

The first commit message states the breaking change. The PR title says it too, because release notes come from the squash commit subject.

Old runs and rollback: the change runs only in the model Activity's constructor on the Worker. It adds no command and changes no Workflow-side value, so histories replay on either version, and the previous release is a safe rollback. During a mixed rollout, Workers on the two versions can send model calls to different endpoints.

Checklist

  1. Closes [Bug] openai_agents: the default model provider ignores set_default_openai_client and set_default_openai_key #167

  2. How was this tested:

    • test_chat_completions_model is parametrized with a default_client case: OPENAI_API_KEY and OPENAI_BASE_URL point at an unreachable endpoint, the test client is set with set_default_openai_client, the plugin has no model_provider, and the model Activity gets one attempt. On main it fails with WorkflowFailureError, caused by a ConnectError to the environment's endpoint. With this change it passes. The default_key case sets OPENAI_API_KEY to another key and the test key with set_default_openai_key, and checks that every model request carries the test key. On main it fails because the request carries the environment's key. With this change it passes. The model_provider case passes on both.
    • The reproduction from [Bug] openai_agents: the default model provider ignores set_default_openai_client and set_default_openai_key #167 exits 1 on temporalio-openai-agents 1.1.0 and on main, and 0 with this change: the default client case goes to /b with key-b and the default key case to /a with key-c, as in native runs.
    • Client settings check: OPENAI_CUSTOM_HEADERS (an Authorization header), OPENAI_ORG_ID and OPENAI_PROJECT_ID are set after the default client is built. With this change the Workflow sends the same headers as a native run (Bearer configured-key, no organization or project), and the client's own retries apply. The first commit, which copied the client with with_options(max_retries=0), sent the environment's key and account headers; the second commit fixes that.
    • Local, in a Linux container with Python 3.13.16 and the locked environment: ruff check --select I, ruff format --check, pyright, mypy, basedpyright and pydocstyle pass, and the full suite passes (388 tests). The container's uv 0.13.1 is outside the range the make targets need, so the environment was made with UV_NO_EDITABLE=1 uv --no-config sync --frozen and the tools ran from the venv. The min Python, macOS and Windows cells did not run locally.
    • Not run against a real model provider.
  3. Any docs updates needed?
    The plugin README's "Model Providers" section has a new paragraph. Release notes must say where model calls now go and how to keep the environment's endpoint and key.

Not covered

  • A run's own RunConfig(model_provider=...) is also ignored in Workflows. That needs its own fix (a clear refusal) and is not in this PR.
  • A default client set after the Worker is built is still ignored; the README says to set it first.
  • The plugin does not force max_retries=0 on a client the application set; openai has no copy that keeps the client's settings without reading the environment again.
  • temporalio 1.34.0 bundles its own copy of the plugin as temporalio.contrib.openai_agents, with the same default client, so it needs the same change in temporalio/sdk-python. On sdk-python main that module only re-exports temporalio.openai_agents and gets the fix from a release of this package.

Open questions

  • Is the change acceptable as the default, or should it be opt-in? Opt-in would leave the documented set_default_openai_client route broken by default.

Without `model_provider`, the plugin built its own `AsyncOpenAI` from
`OPENAI_API_KEY` and `OPENAI_BASE_URL`. A client or key set with the
framework's `set_default_openai_client` or `set_default_openai_key` was
silently ignored: model calls went to the environment's endpoint with the
environment's key, while native runs used the configured client.

The default provider now uses the framework's default client, with its own
retries turned off, or its default key, as native runs do. Without either it
uses the environment as before.

Breaking change: a Worker that sets a framework default client or key and
uses the plugin's default provider now sends its model calls to that client or
key. To keep using the environment's endpoint and key, pass
`model_provider=OpenAIProvider(openai_client=AsyncOpenAI(max_retries=0))` and
a model Activity timeout. The change is Worker-side only; Workflow histories
are not affected.
Copying the client with with_options() builds a new client that reads the
environment again, so its settings could differ from the client the
application set. Use the default client unchanged, as native runs do; its own
max_retries applies, and the README says to set it to 0 to leave retries to
Temporal.
@campoy
campoy marked this pull request as ready for review October 28, 2026 20:03
@campoy
campoy requested a review from a team as a code owner October 28, 2026 20:03
@campoy
campoy requested a review from brianstrauch October 28, 2026 20:03
@campoy

campoy commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Ready for review, @brianstrauch

Copy link
Copy Markdown
Member

Non-blocking suggestion: could we add a regression test for set_default_openai_key with no default client? The new parametrized case covers set_default_openai_client, but the key fallback is a separate branch of this fix. Setting OPENAI_API_KEY to a different value and asserting that the model request uses the key configured through set_default_openai_key would protect that behavior.

Add a default_key case to test_chat_completions_model: OPENAI_API_KEY
holds another key, set_default_openai_key sets the test key, and every
request must carry the test key. Without the fix the request carries
the environment's key.
@campoy

campoy commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, added in c333030. test_chat_completions_model now has a default_key case: OPENAI_API_KEY holds another key, the test key is set with set_default_openai_key, and the mock checks the Authorization header on every request. On main that case fails because the request carries the environment's key.

@campoy
campoy merged commit ba327d4 into main Oct 28, 2026
15 checks passed
@campoy
campoy deleted the fix/openai-agents-python-native-provider-settings-ignored branch October 28, 2026 22:38
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: the default model provider ignores set_default_openai_client and set_default_openai_key

2 participants