Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: support json_schema response_format for chat completions#9

Open
cheulyop wants to merge 1 commit into
EvanZhouDev:main from
cheulyop:cheul/pr-chat-json-schema
Open

fix: support json_schema response_format for chat completions #9
cheulyop wants to merge 1 commit into
EvanZhouDev:main from
cheulyop:cheul/pr-chat-json-schema

Conversation

@cheulyop

@cheulyop cheulyop commented Apr 20, 2026

Copy link
Copy Markdown

Summary

Add json_schema structured output support to the /v1/chat/completions compatibility path.

Today the proxy supports /v1/chat/completions, but it does not correctly honor
response_format: { type: "json_schema", json_schema: ... }. That makes it hard
to use the proxy with clients that expect OpenAI-style structured output on the
Chat Completions endpoint.

This change keeps the scope intentionally narrow:

  • only the Chat Completions path
  • only json_schema response_format handling
  • no Responses SSE normalization changes
  • no toolchain / tsconfig maintenance changes

What changes

  • add response_format typing for chat completion requests
  • detect response_format.type === "json_schema" in the chat completions handler
  • route structured-output requests through generateObject(...)
  • serialize the structured result back into the assistant message content so the
    response remains OpenAI-compatible on the /v1/chat/completions surface
  • add a server test covering json_schema response_format handling

Files in scope

  • packages/openai-oauth/src/chat-completions.ts
  • packages/openai-oauth/src/types.ts
  • packages/openai-oauth/test/server.test.ts

Why this PR is separate

There was a related local change around SSE completed-response reconstruction,
but that is intentionally excluded from this PR so the review stays focused on
one compatibility gap: Chat Completions structured output.

Test plan

  • bun run test

@cheulyop cheulyop marked this pull request as ready for review April 20, 2026 03:16

Copy link
Copy Markdown

Thanks for putting this together. I independently hit the same compatibility gap from the OpenAI Python SDK chat.completions.parse(...) path.

Concrete downstream symptom:

  • client sends /v1/chat/completions
  • request includes response_format: { "type": "json_schema", "json_schema": { ... "strict": true } }
  • Codex generates completion tokens
  • the OpenAI-compatible response has empty/null choices[0].message.content
  • clients relying on parsed structured output fail with "no parsed content"

I verified current HEAD (aa526920af322568968a30fe820b2b9d55545f8a) does not read or translate response_format in chat-completions.ts.

Your PR's generateObject(...) approach is the cleaner upstream-sized fix if it works consistently with the Codex provider. In a local self-hosted deployment I used a lower-level variant that translates chat response_format to Responses API text.format and returns the resulting JSON string as chat message.content. That path was needed for Honcho's deriver because the Python OpenAI SDK only needs valid JSON content back; client-side .parse() then populates .message.parsed.

I also have a working Vitest regression for the chat-to-Responses translation path: a structured chat completion maps to Codex Responses text format and returns populated chat content. I would be happy to contribute that test to this branch if useful.

One thing to consider before merge: the OpenAI SDK can send both:

  • response_format.type = "json_schema"
  • response_format.type = "json_object"

If json_object remains unsupported, it would be useful to reject it explicitly or add a small pass-through test so callers do not get silent unstructured output.

The skeptical-maintainer question I would ask is: "Does this promise OpenAI-grade schema enforcement even if Codex does not support the exact same semantics?" I do not think it has to. The compatibility contract that matters for many clients is narrower: honor the structured-output request well enough to return JSON in message.content, or fail clearly rather than ignoring response_format.

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

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /