Skip to content

Conversation

@jalexy12
Copy link

Summary

When web_fetch returns an error (e.g., url_not_allowed or url_not_accessible), the SDK crashes with:

SyntaxError: JSON Parse error: Unexpected identifier "object"

Root Cause

In convert-to-anthropic-messages-prompt.ts, the error handling assumes output.value is always a JSON string:

const errorValue = JSON.parse(output.value as string);

However, output.value is already an object:

{"type":"error-json","value":{"type":"web_fetch_tool_result_error","errorCode":"url_not_allowed"}}

When JSON.parse() receives an object, it gets coerced to "[object Object]" and fails.

Fix

Check if the value is a string before parsing:

const errorValue =
  typeof output.value === 'string'
    ? JSON.parse(output.value)
    : output.value;

Testing

Tested locally with @ai-sdk/[email protected] and [email protected]. The agent now handles web_fetch errors gracefully instead of crashing.

When web_fetch returns an error, output.value may already be an object
rather than a JSON string. The current code assumes it's always a string
and calls JSON.parse(), which crashes with:

  SyntaxError: JSON Parse error: Unexpected identifier "object"

This happens because the object gets coerced to "[object Object]" string.

Fix: Check if value is string before parsing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant