{"record":{"id":"bf471323ba1c24a7","repo":"langchain-ai/deepagents","slug":"response-schema-exceeds-maximum-nesting-depth-of","errorCode":null,"errorMessage":"response_schema exceeds maximum nesting depth of {_SCHEMA_MAX_DEPTH}","messagePattern":"response_schema exceeds maximum nesting depth of (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/partners/quickjs/langchain_quickjs/_subagent.py","lineNumber":290,"sourceCode":"    return output\n\n\ndef _validate_response_schema(schema: dict[str, Any]) -> None:\n    \"\"\"Reject schemas that exceed size, depth, or property-count limits.\"\"\"\n    serialized = json.dumps(schema)\n    if len(serialized) > _SCHEMA_MAX_BYTES:\n        msg = (\n            f\"response_schema exceeds {_SCHEMA_MAX_BYTES}\"\n            f\" byte limit ({len(serialized)} bytes)\"\n        )\n        raise ValueError(msg)\n\n    def _check(node: dict[str, Any], depth: int, prop_count: list[int]) -> None:\n        if depth > _SCHEMA_MAX_DEPTH:\n            msg = (\n                f\"response_schema exceeds maximum nesting depth of {_SCHEMA_MAX_DEPTH}\"\n            )\n            raise ValueError(msg)\n        props = node.get(\"properties\")\n        if isinstance(props, dict):\n            prop_count[0] += len(props)\n            if prop_count[0] > _SCHEMA_MAX_PROPERTIES:\n                msg = (\n                    \"response_schema exceeds maximum of\"\n                    f\" {_SCHEMA_MAX_PROPERTIES} properties\"\n                )\n                raise ValueError(msg)\n            for value in props.values():\n                if isinstance(value, dict):\n                    _check(value, depth + 1, prop_count)\n        items = node.get(\"items\")\n        if isinstance(items, dict):\n            _check(items, depth + 1, prop_count)\n\n    _check(schema, 0, [0])\n","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/partners/quickjs/langchain_quickjs/_subagent.py#L272-L308","documentation":"During `response_schema` validation, the recursive `_check` walker enforces `_SCHEMA_MAX_DEPTH` on nesting. A schema nested deeper than the limit raises `ValueError`, preventing pathological or accidental deep nesting that would blow up prompts or provider limits.","triggerScenarios":"Passing a `response_schema` whose `properties`/`items` chains exceed `_SCHEMA_MAX_DEPTH` levels to `task()`; recursively defined or self-referential schema fragments expanded too far.","commonSituations":"Generating schemas from deeply nested Python/TypeScript types (e.g. nested dicts of dicts of dicts); transpiling ORM or GraphQL type trees into JSON Schema without flattening.","solutions":["Flatten the schema — replace deep nesting with flatter objects or string/JSON-encoded fields.","Cap the depth when programmatically generating the schema.","Describe only the shallow part of the payload you need and let the model return the rest as text.","Check `_SCHEMA_MAX_DEPTH` for the exact allowed depth before building large schemas."],"exampleFix":"// before\nschema = {\"type\":\"object\",\"properties\":{\"a\":{\"properties\":{\"b\":{\"properties\":{\"c\":{ ... 12 levels ... }}}}}}}\n\n// after\nschema = {\"type\":\"object\",\"properties\":{\"payload_json\":{\"type\":\"string\",\"description\":\"JSON-encoded nested result\"}}}","handlingStrategy":"validation","validationCode":"def schema_depth(node, d=0):\n    m = d\n    for k in (\"properties\", \"items\"):\n        child = node.get(k)\n        if isinstance(child, dict):\n            kids = child.values() if k == \"properties\" else [child]\n            for c in kids:\n                if isinstance(c, dict):\n                    m = max(m, schema_depth(c, d + 1))\n    return m\nassert schema_depth(schema) <= _SCHEMA_MAX_DEPTH, \"schema too deeply nested\"","typeGuard":null,"tryCatchPattern":"try:\n    task(prompt=prompt, response_schema=schema)\nexcept ValueError as e:\n    if \"nesting depth\" in str(e):\n        task(prompt=prompt, response_schema=flatten_schema(schema))","preventionTips":["Cap depth when generating schemas from nested types.","Flatten deep structures into JSON-string fields.","Keep response shapes deliberately shallow."],"tags":["validation","schema","limits","recursion"],"backgroundTag":"schema-validation-failed","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}