{"record":{"id":"f5541f223cf080d9","repo":"mastra-ai/mastra","slug":"a2a-stream-error-json-stringify-parsed-error","errorCode":null,"errorMessage":"A2A stream error - ${JSON.stringify(parsed.error)}","messagePattern":"A2A stream error - (.+?)","errorType":"exception","errorClass":"MastraClientError","httpStatus":200,"severity":"error","filePath":"client-sdks/client-js/src/utils/process-a2a-stream.ts","lineNumber":49,"sourceCode":"  const dataLines = lines.filter(line => line.startsWith('data:')).map(line => line.slice(5).trimStart());\n\n  const payload = dataLines.length > 0 ? dataLines.join('\\n') : trimmedBlock;\n\n  if (!payload || payload === '[DONE]') {\n    return { done: true };\n  }\n\n  let parsed: unknown;\n  try {\n    parsed = JSON.parse(payload);\n  } catch (error) {\n    throw new Error(\n      `Failed to parse A2A stream event: ${error instanceof Error ? error.message : 'unknown parse error'}`,\n    );\n  }\n\n  if (parsed && typeof parsed === 'object' && 'error' in parsed && parsed.error) {\n    throw new MastraClientError(200, 'OK', `A2A stream error - ${JSON.stringify(parsed.error)}`, parsed.error);\n  }\n\n  if (parsed && typeof parsed === 'object' && 'result' in parsed) {\n    return { event: parsed.result as T };\n  }\n\n  return { event: parsed as T };\n}\n\nexport async function* processA2AStream<T = A2AStreamEventData>(\n  stream: globalThis.ReadableStream<Uint8Array>,\n): AsyncGenerator<T, void, undefined> {\n  const reader = stream.getReader();\n  const decoder = new TextDecoder();\n  let buffer = '';\n\n  try {\n    while (true) {","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/client-sdks/client-js/src/utils/process-a2a-stream.ts#L31-L67","documentation":"After successfully parsing an A2A stream event block, parseEventBlock checks for an 'error' field in the JSON object. If present, it throws MastraClientError with HTTP status 200 (since the transport itself succeeded) and a message embedding the JSON-stringified error payload from the remote agent.","triggerScenarios":"Any A2A stream event emitted by the remote agent that contains a truthy \"error\" property — e.g. the agent reports a task failure, unsupported operation, authentication failure, or internal exception serialized as { error: {...} } in the stream.","commonSituations":"Remote A2A agent rejecting the request (bad API key, missing skill), agent-side runtime exception during task execution, version mismatch where the server sends error shapes the client surfaces verbatim, or sending an invalid payload/params the agent cannot process.","solutions":["Read the error detail from MastraClientError (the parsed.error is attached as the error context/domain) to identify the remote cause.","Verify A2A authentication/credentials sent to the remote agent are valid.","Validate request params (message, skill id, task id) against the remote agent's Agent Card requirements.","Check the remote agent service health/logs; the failure originated server-side.","Ensure client and server A2A protocol versions match (Agent Card protocol version vs client)."],"exampleFix":"// before\nconst events = await client.getA2ATools(...); // surfaces MastraClientError: A2A stream error - {\"code\":-32601,...}\n// after\ncatch (e) {\n  if (e instanceof MastraClientError) console.error('A2A remote error:', e.error);\n  // fix request per remote error code, then retry\n}","handlingStrategy":"try-catch","validationCode":"// inspect events for embedded errors before consuming result\nevent.payload && typeof event.payload === 'object' && 'error' in event.payload\n  ? reject(new Error('Remote A2A error: ' + JSON.stringify(event.payload.error)))\n  : accept(event.payload);","typeGuard":"function isA2AErrorEvent(v: unknown): v is { error: unknown } {\n  return typeof v === 'object' && v !== null && 'error' in v && (v as any).error;\n}","tryCatchPattern":"try {\n  for await (const ev of a2aStream) handle(ev);\n} catch (e) {\n  if (e instanceof MastraClientError && e.message.startsWith('A2A stream error')) {\n    console.error('Remote agent failed:', e.error ?? e.message);\n    // surface to user or retry with corrected request\n  } else throw e;\n}","preventionTips":["Validate request params against the remote agent's Agent Card before streaming.","Keep A2A credentials/keys valid and rotated.","Monitor remote agent health; the error originates server-side.","Handle MastraClientError.error (embedded detail) explicitly in your error UI."],"tags":["a2a","streaming","remote-error","client"],"backgroundTag":"remote-stream-error","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}