Repository navigation
fix: openAi model names and structure parsing #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4678e16
fix: fix up model names in openai
AlemTuzlak 5be0608
changeset
AlemTuzlak 433e57a
fix for openai structured outputs
AlemTuzlak 99203e1
remove oneOf from conversion
AlemTuzlak 8ac9059
Update packages/typescript/ai-openai/tests/schema-converter.test.ts
AlemTuzlak 6dd789b
Update .changeset/full-stars-drop.md
AlemTuzlak ef31160
ci: apply automated fixes
autofix-ci[bot] f99133d
throw on providing oneOf
AlemTuzlak 9ea4674
Merge branch '173-gpt-5-chat-model-names-are-incorrect' of https://giβ¦
AlemTuzlak abf1f04
add chat models to openai exports
AlemTuzlak cda4846
Merge branch 'main' into 173-gpt-5-chat-model-names-are-incorrect
AlemTuzlak f9239fb
Merge branch 'main' into 173-gpt-5-chat-model-names-are-incorrect
AlemTuzlak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@tanstack/ai-openai': patch | ||
| '@tanstack/ai': patch | ||
| '@tanstack/ai-solid': patch | ||
| --- | ||
|
|
||
| Fix up model names for OpenAI and release the new response API's | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,7 +61,7 @@ export function makeOpenAIStructuredOutputCompatible( | |
| const prop = properties[propName] | ||
| const wasOptional = !originalRequired.includes(propName) | ||
|
|
||
| // Recursively transform nested objects/arrays | ||
| // Recursively transform nested objects/arrays/unions | ||
| if (prop.type === 'object' && prop.properties) { | ||
| properties[propName] = makeOpenAIStructuredOutputCompatible( | ||
| prop, | ||
|
|
@@ -75,6 +75,12 @@ export function makeOpenAIStructuredOutputCompatible( | |
| prop.items.required || [], | ||
| ), | ||
| } | ||
| } else if (prop.anyOf || prop.oneOf) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to notice,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah you're right, thank you! Wanted to cover both while I was at it π |
||
| // Handle anyOf/oneOf at property level (union types) | ||
| properties[propName] = makeOpenAIStructuredOutputCompatible( | ||
| prop, | ||
| prop.required || [], | ||
| ) | ||
| } else if (wasOptional) { | ||
| // Make optional fields nullable by adding null to the type | ||
| if (prop.type && !Array.isArray(prop.type)) { | ||
|
|
@@ -106,5 +112,19 @@ export function makeOpenAIStructuredOutputCompatible( | |
| ) | ||
| } | ||
|
|
||
| // Handle anyOf (union types) - each variant needs to be transformed | ||
| if (result.anyOf && Array.isArray(result.anyOf)) { | ||
| result.anyOf = result.anyOf.map((variant) => | ||
| makeOpenAIStructuredOutputCompatible(variant, variant.required || []), | ||
| ) | ||
| } | ||
|
|
||
| // Handle oneOf (discriminated unions) - each variant needs to be transformed | ||
| if (result.oneOf && Array.isArray(result.oneOf)) { | ||
| result.oneOf = result.oneOf.map((variant) => | ||
| makeOpenAIStructuredOutputCompatible(variant, variant.required || []), | ||
| ) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
183 changes: 183 additions & 0 deletions
183
packages/typescript/ai-openai/tests/schema-converter.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| makeOpenAIStructuredOutputCompatible, | ||
| transformNullsToUndefined, | ||
| } from '../src/utils/schema-converter' | ||
|
|
||
|
AlemTuzlak marked this conversation as resolved.
|
||
| describe('transformNullsToUndefined', () => { | ||
| it('should convert null to undefined', () => { | ||
| expect(transformNullsToUndefined(null)).toBe(undefined) | ||
| }) | ||
|
|
||
| it('should handle nested objects with null values', () => { | ||
| const input = { a: 'hello', b: null, c: { d: null, e: 'world' } } | ||
| const result = transformNullsToUndefined(input) | ||
| expect(result).toEqual({ a: 'hello', c: { e: 'world' } }) | ||
| }) | ||
|
|
||
| it('should handle arrays with null values', () => { | ||
| const input = [1, null, 3] | ||
| const result = transformNullsToUndefined(input) | ||
| expect(result).toEqual([1, undefined, 3]) | ||
| }) | ||
|
|
||
| it('should preserve non-null values', () => { | ||
| const input = { a: 'string', b: 123, c: true, d: [1, 2, 3] } | ||
| const result = transformNullsToUndefined(input) | ||
| expect(result).toEqual(input) | ||
| }) | ||
| }) | ||
|
|
||
| describe('makeOpenAIStructuredOutputCompatible', () => { | ||
| it('should add additionalProperties: false to object schemas', () => { | ||
| const schema = { | ||
| type: 'object', | ||
| properties: { | ||
| name: { type: 'string' }, | ||
| }, | ||
| required: ['name'], | ||
| } | ||
|
|
||
| const result = makeOpenAIStructuredOutputCompatible(schema, ['name']) | ||
| expect(result.additionalProperties).toBe(false) | ||
| }) | ||
|
|
||
| it('should make all properties required', () => { | ||
| const schema = { | ||
| type: 'object', | ||
| properties: { | ||
| name: { type: 'string' }, | ||
| age: { type: 'number' }, | ||
| }, | ||
| required: ['name'], | ||
| } | ||
|
|
||
| const result = makeOpenAIStructuredOutputCompatible(schema, ['name']) | ||
| expect(result.required).toEqual(['name', 'age']) | ||
| }) | ||
|
|
||
| it('should make optional fields nullable', () => { | ||
| const schema = { | ||
| type: 'object', | ||
| properties: { | ||
| name: { type: 'string' }, | ||
| nickname: { type: 'string' }, | ||
| }, | ||
| required: ['name'], | ||
| } | ||
|
|
||
| const result = makeOpenAIStructuredOutputCompatible(schema, ['name']) | ||
| expect(result.properties.name.type).toBe('string') | ||
| expect(result.properties.nickname.type).toEqual(['string', 'null']) | ||
| }) | ||
|
|
||
| it('should handle anyOf (union types) by transforming each variant', () => { | ||
| const schema = { | ||
| type: 'object', | ||
| properties: { | ||
| u: { | ||
| anyOf: [ | ||
| { | ||
| type: 'object', | ||
| properties: { a: { type: 'string' } }, | ||
| required: ['a'], | ||
| }, | ||
| { | ||
| type: 'object', | ||
| properties: { b: { type: 'number' } }, | ||
| required: ['b'], | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| required: ['u'], | ||
| } | ||
|
|
||
| const result = makeOpenAIStructuredOutputCompatible(schema, ['u']) | ||
|
|
||
| // Each variant in anyOf should have additionalProperties: false | ||
| expect(result.properties.u.anyOf[0].additionalProperties).toBe(false) | ||
| expect(result.properties.u.anyOf[1].additionalProperties).toBe(false) | ||
| }) | ||
|
AlemTuzlak marked this conversation as resolved.
|
||
|
|
||
| it('should handle oneOf (discriminated unions) by transforming each variant', () => { | ||
| const schema = { | ||
| type: 'object', | ||
| properties: { | ||
| u: { | ||
| oneOf: [ | ||
| { | ||
| type: 'object', | ||
| properties: { type: { const: 'a' }, value: { type: 'string' } }, | ||
| required: ['type', 'value'], | ||
| }, | ||
| { | ||
| type: 'object', | ||
| properties: { type: { const: 'b' }, count: { type: 'number' } }, | ||
| required: ['type', 'count'], | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| required: ['u'], | ||
| } | ||
|
|
||
| const result = makeOpenAIStructuredOutputCompatible(schema, ['u']) | ||
|
|
||
| // Each variant in oneOf should have additionalProperties: false | ||
| expect(result.properties.u.oneOf[0].additionalProperties).toBe(false) | ||
| expect(result.properties.u.oneOf[1].additionalProperties).toBe(false) | ||
| }) | ||
|
|
||
| it('should handle nested objects inside anyOf', () => { | ||
| const schema = { | ||
| type: 'object', | ||
| properties: { | ||
| data: { | ||
| anyOf: [ | ||
| { | ||
| type: 'object', | ||
| properties: { | ||
| nested: { | ||
| type: 'object', | ||
| properties: { x: { type: 'string' } }, | ||
| required: ['x'], | ||
| }, | ||
| }, | ||
| required: ['nested'], | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| required: ['data'], | ||
| } | ||
|
|
||
| const result = makeOpenAIStructuredOutputCompatible(schema, ['data']) | ||
|
|
||
| // The nested object inside anyOf variant should also have additionalProperties: false | ||
| expect(result.properties.data.anyOf[0].additionalProperties).toBe(false) | ||
| expect( | ||
| result.properties.data.anyOf[0].properties.nested.additionalProperties, | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('should handle arrays with items', () => { | ||
| const schema = { | ||
| type: 'object', | ||
| properties: { | ||
| items: { | ||
| type: 'array', | ||
| items: { | ||
| type: 'object', | ||
| properties: { id: { type: 'string' } }, | ||
| required: ['id'], | ||
| }, | ||
| }, | ||
| }, | ||
| required: ['items'], | ||
| } | ||
|
|
||
| const result = makeOpenAIStructuredOutputCompatible(schema, ['items']) | ||
| expect(result.properties.items.items.additionalProperties).toBe(false) | ||
| }) | ||
| }) | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.