{"record":{"id":"489c9e11f0a74dab","repo":"mastra-ai/mastra","slug":"we-could-not-convert-the-schema-to-a-jsonschema","errorCode":null,"errorMessage":"We could not convert the schema to a JSONSchema","messagePattern":"We could not convert the schema to a JSONSchema","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/loop/network/index.ts","lineNumber":45,"sourceCode":"import { createStep } from '../../workflows/workflow';\nimport { PRIMITIVE_TYPES } from '../types';\nimport { pruneAgentLoopSnapshot } from '../workflows/prune-snapshot';\n\n/**\n * Convert a schema (PublicSchema) to JSON Schema.\n * Handles Zod v4, AI SDK schemas, JSON Schema, and StandardSchemaWithJSON.\n */\nfunction schemaToJsonSchema(schema: PublicSchema): unknown {\n  if (isStandardSchemaWithJSON(schema)) {\n    return standardSchemaToJSONSchema(schema);\n  }\n\n  // Try to convert raw Zod v4 schema to StandardSchema\n  try {\n    const standardSchema = toStandardSchema(schema);\n    return standardSchemaToJSONSchema(standardSchema);\n  } catch {\n    throw new Error('We could not convert the schema to a JSONSchema');\n  }\n}\nimport type { CompletionConfig, CompletionContext } from './validation';\nimport {\n  runValidation,\n  formatCompletionFeedback,\n  runDefaultCompletionCheck,\n  generateFinalResult,\n  generateStructuredFinalResult,\n} from './validation';\n\nconst OBSERVATIONAL_MEMORY_NETWORK_ERROR =\n  'Observational Memory is not supported with agent network. Agent network does not propagate the threadId/resourceId context Observational Memory requires. Disable observationalMemory before using agent.network().';\n\nfunction isObservationalMemoryEnabled(config: unknown): boolean {\n  if (config === true) return true;\n  if (!config || config === false) return false;\n  if (typeof config !== 'object') return false;","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/loop/network/index.ts#L27-L63","documentation":"schemaToJsonSchema converts a (Zod v4) schema to JSON Schema via toStandardSchema + standardSchemaToJSONSchema. If either conversion throws for any reason, the original error is swallowed and a generic Error 'We could not convert the schema to a JSONSchema' is raised. Callers listed (workflowList, toolList, workflowStep, finalResult) use it to build completion feedback schemas.","triggerScenarios":"Passing a schema that toStandardSchema or standardSchemaToJSONSchema cannot handle: unsupported Zod types/transforms, a non-Zod object passed as a schema, cyclic references, or a schema built with features unsupported by the converter.","commonSituations":"Using advanced Zod v4 features (custom transforms, branded types with unsupported internals) in tool/workflow parameter schemas; passing a Zod v3 schema or plain JSON Schema object where Zod v4 is expected; typo passing the wrong variable as the schema.","solutions":["Simplify the schema: replace custom transforms/unsupported types with plain Zod objects/arrays/primitives","Confirm you are using Zod v4 schemas as expected by this code path","Debug by calling toStandardSchema(schema) yourself to see the swallowed original error","If you already have JSON Schema, use an API path that accepts JSON Schema directly"],"exampleFix":"// before\nconst schema = z.object({\n  id: z.string().transform(s => s.trim()),\n}); // transform breaks conversion\n// after\nconst schema = z.object({\n  id: z.string(),\n}); // convert after validation instead","handlingStrategy":"try-catch","validationCode":"// Sanity-check the schema is a Zod object before conversion\nimport { z } from 'zod';\nif (!(schema instanceof z.ZodType)) {\n  throw new Error('Expected a Zod v4 schema');\n}","typeGuard":"import { z } from 'zod';\nfunction isZodSchema(v: unknown): v is z.ZodType {\n  return v instanceof z.ZodType;\n}","tryCatchPattern":"try {\n  const jsonSchema = schemaToJsonSchema(schema);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('could not convert the schema')) {\n    // original cause was swallowed; re-convert manually to surface it\n    const std = toStandardSchema(schema); // throws the real error\n    console.error('Schema conversion failed at:', std);\n  }\n  throw e;\n}","preventionTips":["Keep tool/workflow parameter schemas to plain Zod v4 object/array/primitive types","Avoid transforms, refinements with unsupported internals, and branded types in schema boundaries","Pin Zod v4 and test schema conversion in CI for each tool/workflow you register","Re-run toStandardSchema yourself when debugging — the thrown message swallows the root cause"],"tags":["schema","zod","json-schema","validation"],"backgroundTag":"schema-conversion-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}