fix(llm): normalize JSON schema for OpenAI strict mode in chat_typed (#1656)#1670
Merged
fix(llm): normalize JSON schema for OpenAI strict mode in chat_typed (#1656)#1670
Conversation
…1656) Add inline_refs_openai() to resolve $ref/$defs before sending to OpenAI, and normalize_for_openai_strict() to add additionalProperties: false and include all properties in required on every object schema. This fixes 400 Bad Request errors when using OpenAI providers for graph entity extraction.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
inline_refs_openai()to resolve$ref/$defsbefore sending schema to OpenAI (OpenAI does not support JSON Schema$defs)normalize_for_openai_strict()to addadditionalProperties: falseand move all properties intorequiredon every object schema node (including nested)chat_typedonOpenAiProviderbefore constructing theTypedChatRequestanyOf: [{T}, {type: "null"}]patterns forOption<T>fields are preserved as-is (correct OpenAI strict mode representation for nullable fields)Test plan
cargo +nightly fmt --checkpassescargo clippy --workspace --features full -- -D warningspasses (0 warnings)cargo nextest run --config-file .github/nextest.toml --workspace --features full --lib --binspasses (5264 tests)gpt-4o-mini) — no longer returns 400 Bad RequestCloses #1656