fix(agents-core): enum missing type field when not nullable#1015
Merged
seratch merged 1 commit intoopenai:mainfrom Feb 25, 2026
Merged
fix(agents-core): enum missing type field when not nullable#1015seratch merged 1 commit intoopenai:mainfrom
seratch merged 1 commit intoopenai:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 57cc341 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
seratch
approved these changes
Feb 25, 2026
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.
Note: I am new(ish) to this package, so please let me know if I missed something! I followed the contributing guidelines and confirmed that the primary fix resolves the API error from Gemini that we were seeing in my local environment.
Overview
Primary issue:
buildEnuminzodJsonSchemaCompatfallback omitstype, breaking non-OpenAI providersWhen a tool schema uses
z.enum([...]).optional()without.nullable(), the primary schema path (zodResponsesFunctionfrom theopenaiSDK) throws because it enforces the Structured Outputs requirement that.optional()must be paired with.nullable(). This is seemingly expected —@openai/agents-corecatches the error and falls back to its own converter inzodJsonSchemaCompat.ts.The problem is that the fallback's
buildEnumfunction produces{ enum: ["A", "B"] }without atypefield. The primary path correctly produces{ type: "string", enum: ["A", "B"] }, but the fallback does not.The fallback logic is correct enough to work with OpenAI's API (which is lenient about the missing
type), but providers that follow OpenAPI 3.0 conventions reject it:Since the
.optional()without.nullable()pattern is common for tool parameter schemas (where the Structured Outputs constraint doesn't apply), the fallback is the main code path for many real-world tool schemas. This makes the missingtypea practical issue for anyone using non-OpenAI providers via@openai/agents-extensions.Secondary issue: This was noticed when building out the tests for the primary issue. Basically,
buildEnumusesObject.values()for native enum objects, but this method includes the reverse-mapping keys for numeric enumsFor a numeric enum like
enum Priority { Low = 0, High = 1 }, TypeScript compiles this to{ Low: 0, High: 1, "0": "Low", "1": "High" }. The originalObject.values()call returns[0, 1, "Low", "High"]instead of just[0, 1]. This was a pre-existing issue, but adding type inference fix made it visible — the incorrect values would cause the type to be inferred as["string", "number"]instead of"number".Detailed fixes
In
packages/agents-core/src/utils/zodJsonSchemaCompat.ts:Type inference — instead of returning
{ enum: values }at each branch, persistvaluesand infertypeat the end. The logic aligns withparseEnumDefandparseNativeEnumDefin theopenaiSDK's vendoredzod-to-json-schema.Reverse-mapping filter —
Object.values()replaced withresolveNativeEnumValues()for object-valued branches, filtering out TypeScript's numeric reverse-mapping keys. MatchesparseNativeEnumDefL12-L15.Steps to reproduce the primary issue
Expected behavior
buildEnumshould infer and include thetypefrom the enum values (e.g."string"forz.enum(),"number"for numericz.nativeEnum()), matching what the primary path produces.Actual behavior
buildEnumproduces{ enum: [...] }withouttype.Updated tests
type: "string"(previously missing)z.nativeEnum()assertingtype: "number"z.nativeEnum()assertingtype: ["string", "number"]