mastra-ai/mastra · error · MastraError

AGENT_NETWORK_ROUTING_AGENT_INVALID_OUTPUT

AGENT_NETWORK_ROUTING_AGENT_INVALID_OUTPUT

Error message

Routing agent returned undefined for 'object'. This may indicate an issue with the model's response or structured output parsing.

What it means

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.

Source

Thrown at packages/core/src/loop/network/index.ts:796

          primitiveType: 'routing',
          primitiveId: 'routing-agent',
          iteration: iterationCount,
          task: inputData.task,
        });
        return {
          ...base,
          primitiveId: 'none',
          primitiveType: 'none' as const,
          prompt: '',
          selectionReason: 'Aborted',
          conversationContext: [],
        };
      }

      const object = await result.object;

      if (!object) {
        throw new MastraError({
          id: 'AGENT_NETWORK_ROUTING_AGENT_INVALID_OUTPUT',
          domain: ErrorDomain.AGENT_NETWORK,
          category: ErrorCategory.SYSTEM,
          text: `Routing agent returned undefined for 'object'. This may indicate an issue with the model's response or structured output parsing.`,
          details: {
            finishReason: result.finishReason ?? null,
            usage: JSON.stringify(result.usage) ?? null,
          },
        });
      }

      const isComplete = object.primitiveId === 'none' && object.primitiveType === 'none';

      // Extract conversation context from the memory-loaded messages only.
      const conversationContext = filterMessagesForSubAgent(result.rememberedMessages ?? []);

      const endPayload = {
        task: inputData.task,

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Inspect result.finishReason in the error details to identify why no object was produced (error, length, tool-call).
  2. Switch the routing agent to a model that reliably supports structured output / tool-call-style routing.
  3. Simplify the routing schema so the model can comply.
  4. Add retry logic around agent.network() for transient provider failures.
  5. Check for request errors (auth, rate limits) logged around the same time.

Example fix

// before
const routingAgent = new Agent({ name: 'router', model: 'openai/gpt-4o-mini' });
// after: use a model with dependable structured-output support
const routingAgent = new Agent({ name: 'router', model: 'openai/gpt-4o' });
Defensive patterns

Strategy: retry

Validate before calling

const model = await routingAgent.getModel();
if (!model || !('doGenerate' in model)) {
  throw new Error('Routing agent has no valid model configured for structured output');
}

Type guard

function hasRoutingObject(result: { object?: unknown; finishReason?: string }): result is { object: Record<string, unknown>; finishReason?: string } {
  return result.object !== undefined && result.object !== null;
}

Try / catch

try {
  await agent.network(input);
} catch (e) {
  if (e instanceof MastraError && e.id === 'AGENT_NETWORK_ROUTING_AGENT_INVALID_OUTPUT') {
    console.error('routing finishReason:', e.details?.finishReason);
    // retry once or fall back to a stronger routing model
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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').

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/6fda5643eae130e5. Report an issue: GitHub.