Skip to content

feat(js/plugins/compat-oai): add OpenAI Responses transport and non-streaming runner - #6006

Open
cabljac wants to merge 16 commits into
mainfrom
feat/compat-oai-responses-slice1
Open

cabljac wants to merge 16 commits into
mainfrom
feat/compat-oai-responses-slice1

Conversation

@cabljac

@cabljac cabljac commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Part of #6002, closes #6003. Design rationale: design notes on the tracking issue.

Adds a second OpenAI transport to the existing openAI() plugin: the thirteen models OpenAI serves only over /v1/responses (gpt-5-pro, gpt-5.2-pro, gpt-5.4-pro, gpt-5.5-pro, gpt-5-codex, gpt-5.1-codex, gpt-5.1-codex-mini, gpt-5.1-codex-max, gpt-5.2-codex, gpt-5.3-codex, codex-mini-latest, o1-pro, o3-pro) now register and run against that endpoint, in the same namespace with the same action-name scheme. They were unreachable via this plugin before.

  • One RESPONSES_ONLY_MODELS const drives the runtime routing predicate and the openAI.model() type union; suffixed names (o3-pro-2025-06-10, gpt-5-pro-preview) route like their base, deliberately biased toward the transport that works.
  • Non-streaming runner over client.responses.create. store is pinned to false unless set, keeping retention behavior identical to Chat Completions.
  • supports.constrained: 'all', so output schemas go natively through text.format instead of genkit's simulated-constrained prompt injection. Non-object schema roots are wrapped and unwrapped the same way as on Chat Completions (fix(js/plugins/compat-oai): wrap non-object output schema roots for OpenAI #7316), since /v1/responses also rejects them. Prior model turns replay part by part, so structured-output history survives multi-turn.
  • Leading system messages are hoisted into instructions (joined with any passthrough instructions) and later ones are sent as developer items; usage reports reasoning and cached tokens; config.timeout is forwarded as an SDK request option; stream and background in config are rejected.
  • Fails loudly instead of degrading: genkit tools and function_call output items are rejected (INVALID_ARGUMENT/UNIMPLEMENTED) until the tool-calling slice, as is any other output item the runner can't represent (local_shell_call, computer_call, mcp_approval_request, image_generation_call, ...); a failed response throws its error instead of returning an empty completion. Deliberate exception: a streaming caller gets the completed response as a single terminal chunk (the Dev UI always streams), with the event protocol in a follow-up.
  • Model listing: main hides pro and codex ids from the list since Chat Completions can't serve them. The curated Responses-only ids now list as Responses models; other pro and codex ids stay hidden.

Usage

import { genkit, z } from 'genkit';
import { openAI } from '@genkit-ai/compat-oai/openai';

const ai = genkit({ plugins: [openAI()] });

// Previously unreachable models now work by name:
const { text } = await ai.generate({
  model: openAI.model('gpt-5-pro'),
  prompt: 'Prove there are infinitely many primes.',
});

// Output schemas go natively through text.format:
const { output } = await ai.generate({
  model: openAI.model('gpt-5-pro'),
  prompt: 'Extract the invoice fields.',
  output: { schema: z.object({ total: z.number(), currency: z.string() }) },
});

Shared-layer note (affects deepSeek()/xai()/openAICompatible()): transport becomes a reserved config key in the Chat Completions body builder - stripped from the wire (Chat Completions 400s on unknown args), with transport: 'responses' rejected via a provider-neutral INVALID_ARGUMENT rather than silently ignored.

No behavior change for existing models: nothing in SUPPORTED_GPT_MODELS changes transport, raw shape, or config; gpt.ts is untouched.

Not in this slice (see #6002): streaming events, tool calling, encrypted reasoning round-trip, annotations, responsesModel() opt-in for dual-transport models, background models.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for the OpenAI Responses API (/v1/responses) in the compat-oai plugin, enabling integration with models served exclusively over this transport (such as gpt-5-pro and o3-pro). It refactors common utilities into utils.ts and adds comprehensive unit and live integration tests. The reviewer feedback suggests replacing generic Error instances with framework-specific GenkitError classes across several validation checks in responses.ts to ensure consistent error handling and telemetry integration.

Comment thread js/plugins/compat-oai/src/responses.ts Resolved
Comment thread js/plugins/compat-oai/src/responses.ts Resolved
Comment thread js/plugins/compat-oai/src/responses.ts Resolved
Comment thread js/plugins/compat-oai/src/responses.ts Resolved
Comment thread js/plugins/compat-oai/src/responses.ts Resolved
@cabljac
cabljac force-pushed the feat/compat-oai-responses-slice1 branch from f276a29 to b468a84 Compare August 10, 2026 17:01
@cabljac
cabljac force-pushed the feat/compat-oai-responses-slice1 branch from b468a84 to d3bfa43 Compare August 10, 2026 17:30
// Silently ignoring the request would hand back a Chat Completions response
// to a caller who asked for something else. This builder is shared by every
// OpenAI-compatible provider, so the message stays provider-neutral.
if (transport === 'responses') {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fires for deepSeek/xai too - none of the providers on this builder can speak the responses transport, and stripping the key silently seemed worse than rejecting it. Hence the provider-neutral message.

Comment thread js/plugins/compat-oai/src/responses.ts Outdated
response,
request.output?.format === 'json'
);
// The Responses event protocol is not mapped yet, so a streaming caller

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weakest part of the slice: streaming callers get the final response as one chunk. Rejecting streaming outright would have made all eight models unusable in the Dev UI, which always streams. Event mapping is #6004.

@cabljac

cabljac commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

Note: unit and fake-server coverage is in (146 tests, including wire assertions for both transports and an end-to-end generate() with an output schema), but the live smoke tests against the real API have not been run yet. Keeping this as draft until that's done.

Comment thread js/plugins/compat-oai/src/responses.ts Resolved
@MichaelDoyle

Copy link
Copy Markdown
Contributor

@cabljac checking in on this OpenAI Responses API stack (#6006, #6148, #6160) — once #7201 lands, is this ready for a rebase and the live OPENAI_API_KEY smoke tests so we can get the stack reviewed and unblock genkit-dart#372?

@adesinah
adesinah force-pushed the feat/compat-oai-responses-slice1 branch from 715afb6 to 1ca24c3 Compare November 18, 2026 21:22
@github-actions github-actions Bot added the docs Improvements or additions to documentation label Nov 18, 2026
@adesinah
adesinah force-pushed the feat/compat-oai-responses-slice1 branch from 1ca24c3 to 7d20f35 Compare November 19, 2026 12:19
@adesinah
adesinah marked this pull request as ready for review November 19, 2026 12:31
@cabljac

cabljac commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@cabljac checking in on this OpenAI Responses API stack (#6006, #6148, #6160) — once #7201 lands, is this ready for a rebase and the live OPENAI_API_KEY smoke tests so we can get the stack reviewed and unblock genkit-dart#372?

Hey thanks! Yes I ran the live smoke tests against the API across the whole stack, all green:

PR Unit Live (OPENAI_LIVE_TESTS=1) Dev UI cases
#6006 186 pass 2/2 9/9 (gpt-5-pro, o3-pro)
#6148 214 pass 4/4 8/8 (streaming, tool round-trip, encrypted reasoning round-trip)
#6160 230 pass 4/4 15/15 (responsesModel(), transport via config and dotprompt frontmatter)

Screenshots below. Before the stack is ready for review, I still need to answer adesinah's open threads on #6160. The live run confirmed a few of them (injected text order with context + output schema, reasoningEffort values outside the SDK union). I'll push fixes for those.

Once #7201 lands I'll rebase the stack straight away.

pr-6148-grid pr-6006-grid pr-6160-grid

… API

Prepares the plugin to route some models over /v1/responses without
changing anything about the models it serves today.

A `transport` config key joins the plugin-level keys that the shared Chat
Completions body builder destructures away, so it can never reach a wire
that 400s on unrecognised arguments. Asking a Chat Completions model for
`transport: 'responses'` is rejected outright rather than silently ignored,
since opting a dual-transport model onto Responses is not available yet.
The OpenAI APIError to GenkitError mapping and the media-part helpers move
to utils.ts so a second runner can share them rather than fork them.

`UNSUPPORTED_MODEL_MATCHERS` matched 'codex' as a bare substring, which hid
gpt-5-codex, gpt-5.1-codex-max and codex-mini-latest from the model list
along with the legacy code-* completion models it was aimed at. The
matchers are now anchored patterns.
cabljac and others added 15 commits December 3, 2026 13:53
Eight OpenAI models - gpt-5-pro, the gpt-5/gpt-5.1 codex family,
codex-mini-latest, o1-pro and o3-pro - are served only over /v1/responses
and so cannot be reached through this plugin at all today.
`RESPONSES_ONLY_MODELS` names them, and a single predicate over that const
drives both the runtime routing and the `openAI.model()` type union. Any
suffixed form of a curated name matches, so o3-pro-2025-06-10 and
gpt-5-pro-preview route like their base rather than failing with an opaque
OpenAI 400; unknown suffixes fail toward the transport that works.

The runner mirrors the Chat Completions one: genkit messages become `input`
items with system messages hoisted into `instructions`, output formats map
onto `text.format`, unrecognised config keys pass through, and the response
`output` items become parts. Prior model turns are serialized part by part
rather than flattened to their text, so a structured-output turn replays as
its JSON instead of as an empty assistant message. The models declare
`constrained: 'all'` so genkit sends the output schema through
`text.format` instead of simulating it in the prompt. `store` is pinned to
false unless the caller sets it, so this transport retains no more data than
the existing one. All eight models register through it, and the resolver,
listActions and `openAI.model()` all route by the same predicate.

Streaming, tool calling and the encrypted reasoning round-trip are not
implemented yet. A streaming caller gets the completed response delivered
as a single chunk; a request carrying tools, a response asking for a tool
call, and a failed response carrying an `error` are all raised rather than
quietly returned as an ordinary answer.
… json and unexplained failures

Json-mode output truncated by max_output_tokens or a content filter left
partial JSON in output_text; JSON.parse threw a raw SyntaxError instead
of letting the finish reason report the truncation. Parse is now
best-effort (JSON.parse, then parsePartialJson, then a text part).

A failed response with a null error slipped past the status/error guard
and surfaced as a successful empty completion; failed responses now
always throw, with a generic message when the API omits the payload.
…d system messages

config.instructions rode the passthrough spread, which lands after the
instructions built from system messages - setting both silently dropped
the system prompt. The two sources are now joined in order.
… output

The Responses API validates json_schema formats under strict mode unless
strict: false is sent - unlike Chat Completions, where strict defaults
off. Genkit output schemas carry no additionalProperties: false, so
every schema-bearing request 400ed with invalid_json_schema. Found by
live smoke against gpt-5-pro; the fix was verified the same way.
… config

Either key through the passthrough changes the response envelope out from
under the non-streaming runner: the SDK hands back a Stream or a queued
response, which fromOpenAIResponse reads as an empty message with a clean
stop finish. Streaming belongs to Genkit's own streaming API and
background responses are not supported yet, so both are rejected.
…sponses usage

The Responses usage payload details reasoning and cached prompt tokens,
and genkit's usage schema has thoughtsTokens and cachedContentTokens for
them; a reasoning model's dominant token cost was reported nowhere.
… config

Assigning body.text wholesale silently discarded a passthrough text
object, so structured output could not be combined with e.g. verbosity;
the reasoning and instructions fields already compose for the same
reason.
…l lineup

Adds gpt-5.2-pro, gpt-5.5-pro, gpt-5.2-codex and gpt-5.3-codex, all
served exclusively over /v1/responses; the suffix bias cannot catch them
from the existing bases (gpt-5.2-codex does not extend gpt-5-codex-), so
without listing they routed to Chat Completions and failed with an
opaque 400.
@hilariie
hilariie force-pushed the feat/compat-oai-responses-slice1 branch from 7d20f35 to c02348b Compare December 3, 2026 13:03

This branch has not been deployed

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

Labels

docs Improvements or additions to documentation js

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[JS] compat-oai: transport boundary + non-streaming Responses runner

4 participants