Skip to content

Allow injecting an OpenAI client (or headers) into OpenAIProvider / AsyncOpenAIProvider #52

Open

Description

@tommasobolis

Summary

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.:

OpenAIProvider("gpt-5-mini", client=my_openai_client, api="chat_completions")
AsyncOpenAIProvider("gpt-5-mini", client=my_async_openai_client)

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.

Checked against main at e1d4cc9 (0.2.1).

Activity

  1. albertlieyingadrian commented on Nov 14, 2026

    @albertlieyingadrian

    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 main at e1d4cc9)

    What it does:

    • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions