Skip to content

fix(py/openai): route legacy JSON requests by wire capability - #6944

Closed
FOWEPJF255 wants to merge 5 commits into
genkit-ai:mainfrom
FOWEPJF255:FOWEPJF255/fix-openai-json-fallback
Closed

FOWEPJF255 wants to merge 5 commits into
genkit-ai:mainfrom
FOWEPJF255:FOWEPJF255/fix-openai-json-fallback

Conversation

@FOWEPJF255

@FOWEPJF255 FOWEPJF255 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Addresses #6621 for cataloged legacy OpenAI chat models. A JSON request with a schema previously sent strict json_schema to GPT-4 Turbo, which does not support Structured Outputs; a GPT-4 request without a schema sent text and no JSON instruction.

The adapter selects a private wire fallback: prompt-only for the earliest GPT-4 models, json_object for GPT-4 Turbo and the early GPT-4o snapshot, and the existing strict schema path for newer models. The legacy catalog entries remain unconstrained, while newer models retain constrained=ALL. Genkit core now supplies schema instructions for unconstrained models, so the adapter does not duplicate them; schema-less legacy requests still receive a short JSON instruction.

Unlisted OpenAI-compatible model IDs keep their existing behavior. No public API was added.

Validation

  • New request-construction regressions failed in three cases before the original fix and pass after it.
  • py/packages/genkit-openai/tests: 429 passed (--no-cov) on Python 3.12.
  • Ruff lint passed on the four touched Python files; Ruff format check passed with the checkout's CRLF line ending.
  • uv lock --check and git diff --check passed.
  • No live OpenAI request was made; tests verify constructed wire requests and prompt instructions.

Prepared with Codex assistance; the focused diff and checks above were reviewed in this run.

@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 updates the OpenAI model adapter to support JSON output fallbacks for legacy models (such as older GPT-4 snapshots and GPT-4 Turbo) that do not support strict structured outputs. It introduces lists of prompt-only and JSON-object-only models, injecting the schema or JSON instructions into the prompt when necessary, and updates GPT-4's capabilities and tests accordingly. The review feedback suggests optimizing performance by pre-computing a combined set of legacy JSON models at the module level, rather than performing set union operations dynamically on every request.

@FOWEPJF255

Copy link
Copy Markdown
Contributor Author

The three set-union review suggestions were addressed in c377cd3: _LEGACY_JSON_MODELS is precomputed once and used in both request paths. I also merged current main in e012060 to resolve the ModelSupports conflict while preserving the JSON fallback behavior. The focused OpenAI model suite passes (126 tests with the aggregate coverage gate disabled for this single-module run), Ruff passes on the touched Python files, and uv lock --check and git diff --check pass. CI is running on the updated head.

@FOWEPJF255

Copy link
Copy Markdown
Contributor Author

I resolved the merge conflict in the OpenAI model constants and pushed merge commit a3ee1b3. The resolution keeps both this PR's legacy JSON wire fallbacks and main's managed-field hint. The merged checkout passes 494 genkit-openai tests, Ruff lint and formatting checks on the four touched files, and uv lock --check. GitHub now reports the PR mergeable against current main; check-changes and Google CLA pass. The new main commits since the merged base only touch Flask/MCP paths.

@MichaelDoyle

Copy link
Copy Markdown
Contributor

Hi @FOWEPJF255 — heads up that Genkit Python SDK development has moved to https://github.com/genkit-ai/genkit-python! Could you re-open this PR over there (file paths map 1:1 after stripping the leading py/ prefix), and link/close this PR once moved? cc @huangjeff5

@FOWEPJF255

Copy link
Copy Markdown
Contributor Author

While migrating this PR, I checked the current Python tree and genkit-ai/genkit-python#128. That approved catalog refresh removes the legacy model capability entries used here and deliberately resolves those IDs with generic info on compatible hosts, including schema-less JSON mode. A direct port of this PR would reintroduce model-specific fallbacks and conflict with that direction.

The remaining technical distinction is explicit-schema output: this change uses prompt-only output for early GPT-4 and json_object plus prompt instructions for GPT-4 Turbo / the first GPT-4o snapshot, rather than sending json_schema. Would you prefer that behavior retained as a narrowly scoped adapter fix in the new repository, or should this legacy-model fix be retired with the catalog update? I am holding the conflicting migration pending that decision; the Bedrock migration is now genkit-ai/genkit-python#167.

@MichaelDoyle

Copy link
Copy Markdown
Contributor

Thanks for checking against genkit-ai/genkit-python#128! We don't want to maintain hardcoded legacy-model fallbacks for those older GPT-4 / GPT-4 Turbo / initial GPT-4o snapshots, so let's retire this fix with the catalog refresh. Closing this PR.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants