Skip to content

fix(ai): the openai provider without ai.api_base_url calls the OpenAI API's /v1 routes, built by the openai SDK #1427

Description

@dan-petty

Problem

Checked at release/v0.2.29 (05e9388).

  • OpenAICompatProviderMixin._api_base returns CONST_URL_OPENAI_API_BASE, which is https://api.openai.com (config/constants.py:590, ai/client/openai.py:46). The client appends /chat/completions and /models (openai.py:169, :199, :217). The OpenAI API serves those routes under /v1, so provider openai without ai.api_base_url sends every chat, stream and model listing to a route that does not exist.
  • Three other defaults include /v1: ai/providers/openai.py:41, ai/rag/embeddings.py:889 and the review dry-run preview (ai/review/runner.py:210). The preview and the embeddings therefore show a URL that chat never uses.
  • The cause is that the whole request is assembled by hand: the URL, the bearer header (openai.py:48-54), the payload (:81-105), the response parse (:120-153), the SSE event decoding (ai/client/streaming.py:350-385) and the error mapping by status (ai/client/base.py:119-132).
  • The openai SDK (OpenAI; 3.26.1, released 2026-10-08; 3.13.0 is already in uv.lock through pydantic-ai) defaults to https://api.openai.com/v1 and accepts an httpx2.Client as http_client. The factory's egress-checked client (http/client.py:142) can therefore stay the transport, so fix(security): every HTTP client dials only addresses its egress level allows, checked at the connect and on every redirect #898's connect-time check and the HTTP-edge test fakes keep working.

Proposed remedy

  • OpenAICompatProviderMixin holds one openai.OpenAI(base_url=..., api_key=..., http_client=<the provider's factory client>, max_retries=0, timeout=<the client's request timeout>) per client. Chat (chat.completions.create, with stream=True for streams) and model listing (models.list) go through it. Provider openai takes the SDK's default base URL; copilot and gateway keep their configured base URLs.
  • The gateway's served-by header is read from the raw response (with_raw_response, or the stream's response headers).
  • openai.APIStatusError and openai.APIConnectionError map to AIClientError and credentials_error by exception type and status, not by text.
  • Reasoning text (reasoning_content or reasoning in a delta) is read from the SDK chunk's extra fields.
  • Delete CONST_URL_OPENAI_API_BASE, the other hand-written OpenAI defaults, _build_compat_payload, _parse_compat_response, _openai_compat_headers and _openai_stream_frame.
  • Declare openai in pyproject.toml.
  • Unchanged: Ollama slot leasing and request priority (ai/client/network.py), feat(ai): finish reasons and stream completeness for model replies #599's finish reasons and fix(ai): sampling settings reach the model or are refused, review sessions record them, and the reply cache key includes them #1070's sampling rules.

Key questions

Acceptance criteria

  • With provider openai and no ai.api_base_url, the recorded requests go to https://api.openai.com/v1/chat/completions (unstreamed and streamed) and https://api.openai.com/v1/models. The test fails on the current code.
  • With provider copilot, and with provider gateway and ai.gateway_url: https://example.com/v1, the recorded URLs are the configured base plus /chat/completions, as today.
  • The SDK client's HTTP client is the one the factory built (an identity check), so the egress check covers every request it sends.
  • The existing finish-reason, stream-completeness and served-by tests pass against SDK-built requests.
  • rg -n 'api.openai.com' src prints no hand-written default, and rg -n '_openai_stream_frame|_build_compat_payload|_parse_compat_response' src prints nothing.
  • Tests run offline, with fakes only at the HTTP edge (an httpx2 MockTransport, through tests/llm_stream_fakes.route_client or refactor(cli): Unify HTTP and DNS seams with shared fakes #1314's fixture) or the process edge. No test patches a devops_cli name.
  • Each new or changed test runs in under 1 s.
  • uv run devops ci passes.

Related: #1001 (moves this client behind its factory), #888, #1070, #591, #1419, #1019

Found by the 2026-10-08 client-library audit.

Opened as a borrow (#1153, budget/borrowed): the agent filing quota is spent for this cycle, and this is a defect or security finding of the owner-requested 2026-10-08 client-library audit.

Update 2026-10-08: remedy follows the owner's #1001 decision

#1001 now uses pydantic-ai models, not the openai SDK directly, so this item's remedy changes:

  • The openai provider is a pydantic-ai OpenAIChatModel over OpenAIProvider, built with http_client= from the egress factory. It is called through pydantic_ai.direct.model_request_sync and model_request_stream_sync.
  • With no ai.api_base_url, the provider's own default (https://api.openai.com/v1) applies. The hand-written defaults and builders listed above are deleted as planned.
  • The acceptance criteria stand: the recorded requests go to /v1/chat/completions and /v1/models, and the client is the factory-built one (an identity check on the provider's client).

If #1001 lands first, this item is its OpenAI step. Otherwise it lands this provider as #1001's first step.

Activity

  1. added
    type/bug
    scope/ai
    source/agent
    on Oct 8, 2026
  2. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P1-High. The default OpenAI provider is completely broken for any user who does not set ai.api_base_url, affecting every chat, stream, and model-listing call.
    • Effort: High. The fix requires replacing hand-written HTTP, streaming, error-mapping, and payload code with the openai SDK while preserving egress checks, retry accounting, response-size caps, and existing test contracts.
  3. added a commit that references this issue on Oct 26, 2026
    fbcc074
  4. added a commit that references this issue on Oct 26, 2026
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

    scope/aisource/agenttype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions