fix(ai): serialize Date in tool-result json output so Postgres timestamps don't crash multi-tool loops (#2433)#2491
Open
javieraldape wants to merge 1 commit into
Conversation
…amps don't crash multi-tool loops (garrytan#2433) node-postgres returns timestamp columns as JS Date instances. toModelMessages() emitted tool results in the json branch as { type: 'json', value: b.output }, passing those Dates raw; AI SDK v6's jsonValueSchema rejects Date, so any multi-tool/subagent loop that called a brain tool returning timestamps aborted with AI_InvalidPromptError. JSON-roundtrip the value so Dates serialize to ISO strings the validator accepts. Regression test pins the conversion. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
Fixes #2433. Any multi-tool rollout or subagent loop that called a brain tool returning Postgres timestamp columns (e.g.
brain_list_pages,brain_query) crashed AI SDK v6 validation withAI_InvalidPromptError: The messages do not match the ModelMessage[] schema.Root cause
node-postgresdeserializes timestamp columns into JSDateinstances.toModelMessages()(src/core/ai/gateway.ts) emitted the tool result in itsjsonbranch as{ type: 'json', value: b.output }, passing thoseDates through raw. AI SDK v6'sjsonValueSchemaaccepts onlynull | string | number | boolean | record | array— notDate— so the entire message tree was rejected.Fix
JSON-roundtrip the value in the
jsonbranch soDates (including nested ones) serialize to ISO strings the validator accepts:Scope kept tight: the
error-textbranch already stringifies and is untouched; noBigInthandling added (that's a separate concern, #2450).Test
test/gateway-model-messages.test.tsgains a case feeding a tool-result whoseoutputnests aDate, asserting it serializes to the ISO string. Confirmed it fails on the pre-fix code (receives a rawDate) and passes after. The issue author independently verified the fix on a real workload (subagent rollout success 1/5 → 5/5).Verification
bun run typecheck→ clean (EXIT=0)bun test test/gateway-model-messages.test.ts→ all passCloses #2433.
🤖 Generated with Claude Code