fix: allow tool calls with invalid JSON to trigger repair functionality #76
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.
Fix tool call repair functionality for invalid JSON
This PR fixes issue #74 where
experimental_repairToolCall
doesn't get triggered when models generate invalid JSON for tool parameters.Problem
The current implementation in
openrouter-chat-language-model.ts
blocks tool calls with invalid JSON from being sent by checkingisParsableJson(toolCall.function.arguments)
before enqueuing tool calls. This prevents theexperimental_repairToolCall
function from being triggered when it's needed most - when the model generates malformed JSON.Solution
Following the guidance from the linked Vercel AI SDK issue #6520, this PR modifies the streaming tool call logic to:
isParsableJson
checks in the main streaming logic (lines 593-617 and 644-658)'{}'
isParsableJson
to clean up the codeChanges Made
isParsableJson
importTesting
The fix allows the
experimental_repairToolCall
function to receive and handle malformed tool calls as intended by the Vercel AI SDK design. This enables proper error handling and recovery when models generate invalid JSON for tool parameters.Related Issues
streamText
inopenai-compatible
provider vercel/ai#6520Link to Devin run: https://app.devin.ai/sessions/c5b2f9026c42472181120b5fbfac7912
Requested by: Louis ([email protected])