{"record":{"id":"6510b1fcd5f74b5c","repo":"agentscope-ai/agentscope","slug":"invalid-structured-output-from-model-model-name","errorCode":null,"errorMessage":"Invalid structured output from model {model_name}: {e}","messagePattern":"Invalid structured output from model (.+?): (.+?)","errorType":"exception","errorClass":"StructuredOutputError","httpStatus":null,"severity":"error","filePath":"src/agentscope/model/_base.py","lineNumber":731,"sourceCode":"            # Validate the output\n            if isinstance(structured_model, dict):\n                jsonschema.validate(structured_output, structured_model)\n\n            elif issubclass(structured_model, BaseModel):\n                structured_model.model_validate(structured_output)\n\n            else:\n                raise ValueError(\n                    \"The structured_model is expected to be a subclass of \"\n                    \"Pydantic.BaseModel or a dict, \"\n                    f\"but got {type(structured_model)}.\",\n                )\n        except (\n            ToolJSONDecodeError,\n            jsonschema.ValidationError,\n            PydanticValidationError,\n        ) as e:\n            raise StructuredOutputError(\n                f\"Invalid structured output from model {model_name}: {e}\",\n            ) from e\n\n        return StructuredResponse(\n            id=completed_response.id,\n            created_at=completed_response.created_at,\n            content=structured_output,\n            usage=completed_response.usage,\n            finished_reason=completed_response.finished_reason,\n        )\n","sourceCodeStart":713,"sourceCodeEnd":742,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/model/_base.py#L713-L742","documentation":"The model's structured output was parsed but failed validation against the provided schema (JSON schema validation or Pydantic model_validate). The error chains the underlying ValidationError and wraps it in StructuredOutputError with the model name.","triggerScenarios":"Model returns JSON that is missing required fields, has wrong types (string where int expected), extra fields not allowed, or enum values outside the allowed set.","commonSituations":"Strict Pydantic schema with required fields the model omits; model coercing numbers to strings; schemas with additionalProperties=False; vague prompts letting the model guess field formats.","solutions":["Read the chained ValidationError: it names the exact field and violation — fix the prompt or schema accordingly","Make schema fields Optional with defaults so the model isn't forced to fill everything","Add field descriptions and examples in the Pydantic model so the model knows expected formats","Retry the call; wrap in a repair loop that feeds the validation error back to the model"],"exampleFix":"# before\nclass Out(BaseModel):\n    age: int  # model returns \"25\" string -> ValidationError\n\n# after\nfrom pydantic import Field\nclass Out(BaseModel):\n    age: int = Field(..., description='Age in years as an integer, e.g. 25')","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    res = await model.generate_structured_output(msgs, Schema)\nexcept StructuredOutputError as e:\n    if not isinstance(e.__cause__, (ValueError,)) or 'validation' not in str(e.__cause__).lower():\n        raise\n    # repair loop: send validation error back to model\n    msgs.append(Msg('user', f'Fix these validation errors: {e.__cause__}'))","preventionTips":["Make optional fields Optional with defaults","Add Field(description=...) and examples to guide the model","Retry with the validation error appended to the conversation"],"tags":["agentscope","structured-output","pydantic","jsonschema","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}