{"record":{"id":"7499c0d0bb558070","repo":"mastra-ai/mastra","slug":"invalid-input-for-tool-toolid","errorCode":null,"errorMessage":"Invalid input for tool \"${toolId}\"","messagePattern":"Invalid input for tool \"(.+?)\"","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/tools/code-mode/code-mode.ts","lineNumber":116,"sourceCode":"          );\n        }\n      }\n\n      // Each external_* call re-enters the real Mastra tool pipeline (validation,\n      // request-context checks, tracing) on the host, with the outer tool's context.\n      const dispatch: CodeModeToolDispatcher = async (toolId, args) => {\n        const tool = toolsById.get(toolId);\n        if (!tool?.execute) {\n          throw new Error(`Tool \"${toolId}\" is not available in Code Mode`);\n        }\n        const result = await tool.execute(args, {\n          mastra: ctx?.mastra,\n          requestContext: ctx?.requestContext,\n          abortSignal: ctx?.abortSignal,\n          workspace: ctx?.workspace,\n        });\n        if (isValidationError(result)) {\n          throw new Error(result.message ?? `Invalid input for tool \"${toolId}\"`);\n        }\n        return result;\n      };\n\n      // The TypeScript program is written to a .ts module by the transport;\n      // the sandbox's node strips the type annotations natively at import.\n      return ctx.observe.span(`code-mode:${id}`, () =>\n        transport.run({\n          sandbox,\n          program: code,\n          toolIds,\n          dispatch,\n          timeout,\n          abortSignal: ctx?.abortSignal,\n          onExternalCall: (tool, args) => ctx.observe.log('info', 'code-mode external call', { tool, args }),\n          onExternalResult: (tool, durationMs, error) =>\n            ctx.observe.log(error ? 'error' : 'info', 'code-mode external result', { tool, durationMs }),\n        }),","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/tools/code-mode/code-mode.ts#L98-L134","documentation":"After the dispatcher re-enters the real Mastra tool pipeline, a validation-failure result (isValidationError) from the tool's inputSchema check is converted into a thrown error. It means the arguments the model-authored code passed to external_<tool> did not match the tool's input schema.","triggerScenarios":"Generated TypeScript calls external_<toolId> with arguments that fail the tool's Zod/inputSchema validation, e.g. missing required fields, wrong types, or wrong shapes.","commonSituations":"The LLM writes code with guessed argument shapes; schema drift after editing a tool's inputSchema; passing stringified JSON instead of an object; optional vs required field confusion.","solutions":["Inspect result.message (the thrown message when non-empty) to see the schema violation and fix the generated call's arguments.","Improve tool descriptions and input schema descriptions so the model emits correct argument shapes.","Validate/parse arguments in generated code before calling external_* helpers.","Check for recent changes to the tool's inputSchema that the model prompts/stubs no longer reflect."],"exampleFix":"// before (generated code)\nawait external_weather('london');\n// after\nawait external_weather({ location: 'london' });","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  result = await codeModeTool.execute(args, ctx);\n} catch (e) {\n  if (String(e.message).startsWith('Invalid input for tool')) {\n    // feed e.message back to the model to correct the generated arguments\n  } else throw e;\n}","preventionTips":["Write precise inputSchema field descriptions; the model reads them when authoring code.","Parse/validate arguments inside generated code before calling external_* helpers.","Re-run tests when a tool's inputSchema changes."],"tags":["code-mode","input-validation","schema","tool-dispatch"],"backgroundTag":"schema-validation-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}