Repository navigation
fix(ai): undo strict-mode nulls before an approved tool check #2110
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
Changes from all commits
ba57282
a264a86
486f956
16a7602
4ec4410
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@tanstack/ai': minor | ||
| '@tanstack/openai-base': minor | ||
| --- | ||
|
|
||
| Undo strict-mode `null`s on tool arguments before the input check. An approved call whose history still has those `null`s now runs when `repairToolInput` is off. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1010,6 +1010,9 @@ async function buildClientToolResult( | |
| * repaired: the built-in repair (default), none (`false`), or a function. | ||
| * @param capabilities - The run's capability readers. Each server tool gets | ||
| * them on its context as `get` and `getOptional`. | ||
| * @param normalizeToolInput - Undo a provider rewrite of the parsed | ||
| * arguments before the check. Strict-mode `null`s for omitted optional | ||
| * fields are removed here, including on an approval resume. | ||
| */ | ||
| export async function* executeToolCalls<TContext = unknown>( | ||
| toolCalls: Array<ToolCall>, | ||
|
|
@@ -1029,6 +1032,7 @@ export async function* executeToolCalls<TContext = unknown>( | |
| toolExecution: NonNullable<TextOptions['toolExecution']> = 'parallel', | ||
| repairToolInput?: TextOptions['repairToolInput'], | ||
| capabilities?: Pick<ToolExecutionContext, 'get' | 'getOptional'>, | ||
| normalizeToolInput?: (toolName: string, input: unknown) => unknown, | ||
| ): AsyncGenerator<CustomEvent | StreamChunk, ExecuteToolCallsResult, void> { | ||
| const results: Array<ToolResult> = [] | ||
| const needsApproval: Array<ApprovalRequest> = [] | ||
|
|
@@ -1185,6 +1189,7 @@ export async function* executeToolCalls<TContext = unknown>( | |
| continue | ||
| } | ||
| } | ||
| if (normalizeToolInput) input = normalizeToolInput(toolName, input) | ||
|
Contributor
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. Preserve completed results when normalization fails. In sequential mode, an earlier tool can finish before a later call's normalizer throws. The throw prevents
|
||
| // The input that the model sent, to tell later edits apart from it. | ||
| const sentInput = JSON.stringify(input) | ||
|
|
||
|
|
@@ -1411,6 +1416,7 @@ export async function* executeToolCalls<TContext = unknown>( | |
| toolExecution, | ||
| repairToolInput, | ||
| capabilities, | ||
| normalizeToolInput, | ||
| ) | ||
| results.push(...rest.results) | ||
| needsClientExecution.push(...rest.needsClientExecution) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ import type { | |
| ChatCompletionMessageParam, | ||
| } from 'openai/resources/chat/completions/completions' | ||
| import type { | ||
| AnyTool, | ||
| ContentPart, | ||
| DefaultMessageMetadataByModality, | ||
| Modality, | ||
|
|
@@ -844,6 +845,17 @@ export abstract class OpenAIBaseChatCompletionsTextAdapter< | |
| return makeStructuredOutputCompatibleWithMap(schema, originalRequired) | ||
| } | ||
|
|
||
| /** Undo strict-mode nulls before the engine checks recorded arguments. */ | ||
| normalizeToolInput( | ||
| toolName: string, | ||
| input: unknown, | ||
| tools: ReadonlyArray<AnyTool> | undefined, | ||
| ): unknown { | ||
| return createToolInputNormalizer(tools, (schema, required) => | ||
| this.makeStructuredOutputCompatibleWithMap(schema, required), | ||
| )(toolName, input) | ||
|
Comment on lines
+854
to
+856
Contributor
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.
|
||
| } | ||
|
|
||
| /** | ||
| * Applies provider-specific transformations for structured output compatibility. | ||
| * Override `makeStructuredOutputCompatibleWithMap` when you need the inverse map | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: TanStack/ai
Length of output: 2886
Script executed:
Repository: TanStack/ai
Length of output: 44927
Script executed:
Repository: TanStack/ai
Length of output: 36890
Script executed:
Repository: TanStack/ai
Length of output: 42992
Script executed:
Repository: TanStack/ai
Length of output: 30391
Script executed:
Repository: TanStack/ai
Length of output: 18824
Script executed:
Repository: TanStack/ai
Length of output: 22125
Normalize pending calls using their originating provider.
When a call from another provider resumes with OpenAI, this callback can remove an invalid optional
null. WithrepairToolInput: false, the tool can then execute instead of returning a validation error. Apply strict-mode normalization only when the originating call used that rewrite.