{"record":{"id":"6fda5643eae130e5","repo":"mastra-ai/mastra","slug":"agent-network-routing-agent-invalid-output","errorCode":"AGENT_NETWORK_ROUTING_AGENT_INVALID_OUTPUT","errorMessage":"Routing agent returned undefined for 'object'. This may indicate an issue with the model's response or structured output parsing.","messagePattern":"Routing agent returned undefined for 'object'\\. This may indicate an issue with the model's response or structured output parsing\\.","errorType":"error_code","errorClass":"MastraError","httpStatus":null,"severity":"error","filePath":"packages/core/src/loop/network/index.ts","lineNumber":796,"sourceCode":"          primitiveType: 'routing',\n          primitiveId: 'routing-agent',\n          iteration: iterationCount,\n          task: inputData.task,\n        });\n        return {\n          ...base,\n          primitiveId: 'none',\n          primitiveType: 'none' as const,\n          prompt: '',\n          selectionReason: 'Aborted',\n          conversationContext: [],\n        };\n      }\n\n      const object = await result.object;\n\n      if (!object) {\n        throw new MastraError({\n          id: 'AGENT_NETWORK_ROUTING_AGENT_INVALID_OUTPUT',\n          domain: ErrorDomain.AGENT_NETWORK,\n          category: ErrorCategory.SYSTEM,\n          text: `Routing agent returned undefined for 'object'. This may indicate an issue with the model's response or structured output parsing.`,\n          details: {\n            finishReason: result.finishReason ?? null,\n            usage: JSON.stringify(result.usage) ?? null,\n          },\n        });\n      }\n\n      const isComplete = object.primitiveId === 'none' && object.primitiveType === 'none';\n\n      // Extract conversation context from the memory-loaded messages only.\n      const conversationContext = filterMessagesForSubAgent(result.rememberedMessages ?? []);\n\n      const endPayload = {\n        task: inputData.task,","sourceCodeStart":778,"sourceCodeEnd":814,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/loop/network/index.ts#L778-L814","documentation":"In createNetworkLoop, the routing agent is invoked with structured output to decide routing. If result.object resolves to undefined/empty, the network cannot route and throws this SYSTEM-category error, including the model's finishReason in details for diagnosis.","triggerScenarios":"Calling agent.network()/createNetworkLoop where the routing model's generate call resolves with no 'object' — e.g. the model returned no structured output, structured-output parsing failed, the finishReason was an error/length/tool-call instead of 'stop', or the model does not actually support the requested structured-output schema.","commonSituations":"Using a small/cheap model that ignores the routing JSON schema; provider silently downgrades structured output; schema too complex for the model; token limit truncates the response mid-JSON; transient provider failure (finishReason 'error').","solutions":["Inspect result.finishReason in the error details to identify why no object was produced (error, length, tool-call).","Switch the routing agent to a model that reliably supports structured output / tool-call-style routing.","Simplify the routing schema so the model can comply.","Add retry logic around agent.network() for transient provider failures.","Check for request errors (auth, rate limits) logged around the same time."],"exampleFix":"// before\nconst routingAgent = new Agent({ name: 'router', model: 'openai/gpt-4o-mini' });\n// after: use a model with dependable structured-output support\nconst routingAgent = new Agent({ name: 'router', model: 'openai/gpt-4o' });","handlingStrategy":"retry","validationCode":"const model = await routingAgent.getModel();\nif (!model || !('doGenerate' in model)) {\n  throw new Error('Routing agent has no valid model configured for structured output');\n}","typeGuard":"function hasRoutingObject(result: { object?: unknown; finishReason?: string }): result is { object: Record<string, unknown>; finishReason?: string } {\n  return result.object !== undefined && result.object !== null;\n}","tryCatchPattern":"try {\n  await agent.network(input);\n} catch (e) {\n  if (e instanceof MastraError && e.id === 'AGENT_NETWORK_ROUTING_AGENT_INVALID_OUTPUT') {\n    console.error('routing finishReason:', e.details?.finishReason);\n    // retry once or fall back to a stronger routing model\n  }\n  throw e;\n}","preventionTips":["Use a model known to support structured output for the routing agent","Keep the routing schema simple","Monitor finishReason in error details to catch provider errors early"],"tags":["agent-network","structured-output","routing","model-response"],"backgroundTag":"structured-output-parse-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}