Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/typescript/ai-anthropic/src/adapters/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,10 @@ export class AnthropicTextAdapter<
for (const toolCall of message.toolCalls) {
let parsedInput: unknown = {}
try {
parsedInput = toolCall.function.arguments
const parsed = toolCall.function.arguments
? JSON.parse(toolCall.function.arguments)
: {}
parsedInput = parsed && typeof parsed === 'object' ? parsed : {}
} catch {
parsedInput = toolCall.function.arguments
}
Expand Down Expand Up @@ -586,7 +587,8 @@ export class AnthropicTextAdapter<
// Emit TOOL_CALL_END
let parsedInput: unknown = {}
try {
parsedInput = existing.input ? JSON.parse(existing.input) : {}
const parsed = existing.input ? JSON.parse(existing.input) : {}
parsedInput = parsed && typeof parsed === 'object' ? parsed : {}
} catch {
parsedInput = {}
}
Expand Down
10 changes: 7 additions & 3 deletions packages/typescript/ai/src/activities/chat/tools/tool-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ export class ToolCallManager {
let toolResultContent: string
if (tool?.execute) {
try {
// Parse arguments
// Parse arguments (normalize "null" to "{}" for empty tool_use blocks)
let args: unknown
try {
args = JSON.parse(toolCall.function.arguments)
const argsString = toolCall.function.arguments.trim() || '{}'
args = JSON.parse(argsString === 'null' ? '{}' : argsString)
} catch (parseError) {
throw new Error(
`Failed to parse tool arguments as JSON: ${toolCall.function.arguments}`,
Expand Down Expand Up @@ -294,7 +295,10 @@ export async function executeToolCalls(

// Parse arguments, throwing error if invalid JSON
let input: unknown = {}
const argsStr = toolCall.function.arguments.trim() || '{}'
let argsStr = toolCall.function.arguments.trim() || '{}'
// Normalize "null" to "{}" — can occur when the model streams a tool_use
// block with no input_json_delta events (Anthropic adapter edge case)
if (argsStr === 'null') argsStr = '{}'
if (argsStr) {
try {
input = JSON.parse(argsStr)
Expand Down
Loading