You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow injecting an OpenAI client (or headers) into OpenAIProvider / AsyncOpenAIProvider #52
OpenAIProvider and AsyncOpenAIProvider build their own openai.OpenAI / openai.AsyncOpenAI client from base_url and api_key only (providers/openai.py, OpenAIProvider.__init__: openai.OpenAI(base_url=base_url, api_key=api_key, max_retries=0)). There is no way to pass a pre-configured client, default_headers, or an http_client.
Why it matters
OpenAI-compatible gateways and proxies often authenticate with something other than a bearer api_key, such as a client-id/secret header pair, or need extra headers for attribution and correlation. With the current constructor, pointing base_url at such a gateway fails with a 401 because the required headers can't be added.
The workaround today is to subclass the provider and replace the private _client after super().__init__(). That works (I ran System One questions end to end through a governed gateway this way, with both structured_outputs=True and False), but it depends on a private attribute, so it can break on any release.
Proposal
Accept an optional client on both providers, e.g.:
When client is given, use it as is and skip building one (and don't close it in close() if the caller owns it). A lighter alternative is to forward default_headers and http_client to the openai.OpenAI(...) call.
OpenAIProvider(..., client=openai.OpenAI(...)) and AsyncOpenAIProvider(..., client=openai.AsyncOpenAI(...)) use the caller's endpoint, credentials, default_headers and http_client. Responses vs Chat Completions is still detected from the client's host.
The provider stores client.with_options(max_retries=0). That copy shares the caller's connection pool but turns off SDK-level retries, so RetryPolicy stays the only retry layer and the per-attempt debug records stay accurate. A client built with max_retries=3 and a RetryPolicy(max_retries=0) makes exactly one request.
close() / aclose() leave a caller-supplied client open, as described in the proposal. Owned clients are still closed.
Passing client together with base_url or api_key raises ValueError, rather than silently ignoring one of them.
A README paragraph shows the gateway case.
Tests: tests/test_openai_client_injection.py has 14 cases covering sync and async: headers and URL on the wire, Responses on api.openai.com, retries, ownership on close, and the conflict check. On main, 12 of them fail; the other 2 check that an owned client is still closed. The full suite has 438 passing, and pyrefly check reports 0 errors.
Happy to adjust the API shape, for example forwarding only default_headers / http_client if you'd rather not accept a whole client.
Activity
albertlieyingadrian commentedon Nov 14, 2026
I implemented this on a branch, since outside PRs can't be opened here: main...albertlieyingadrian:system-one-adapter-python:feat/openai-provider-client (commit 465aaff, on
mainat e1d4cc9)What it does:
OpenAIProvider(..., client=openai.OpenAI(...))andAsyncOpenAIProvider(..., client=openai.AsyncOpenAI(...))use the caller's endpoint, credentials,default_headersandhttp_client. Responses vs Chat Completions is still detected from the client's host.client.with_options(max_retries=0). That copy shares the caller's connection pool but turns off SDK-level retries, soRetryPolicystays the only retry layer and the per-attempt debug records stay accurate. A client built withmax_retries=3and aRetryPolicy(max_retries=0)makes exactly one request.close()/aclose()leave a caller-supplied client open, as described in the proposal. Owned clients are still closed.clienttogether withbase_urlorapi_keyraisesValueError, rather than silently ignoring one of them.Tests:
tests/test_openai_client_injection.pyhas 14 cases covering sync and async: headers and URL on the wire, Responses onapi.openai.com, retries, ownership on close, and the conflict check. Onmain, 12 of them fail; the other 2 check that an owned client is still closed. The full suite has 438 passing, andpyrefly checkreports 0 errors.Happy to adjust the API shape, for example forwarding only
default_headers/http_clientif you'd rather not accept a whole client.