{"record":{"id":"5d797ca4780e5638","repo":"agentscope-ai/agentscope","slug":"the-structured-model-is-expected-to-be-a-subclass","errorCode":null,"errorMessage":"The structured_model is expected to be a subclass of Pydantic.BaseModel or a dict, but got {type(structured_model)}.","messagePattern":"The structured_model is expected to be a subclass of Pydantic\\.BaseModel or a dict, but got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/agentscope/model/_base.py","lineNumber":721,"sourceCode":"                        _.input,\n                        input_schema,\n                    )\n                    break\n\n            if structured_output is None:\n                raise StructuredOutputError(\n                    \"Failed to generate structured output for model.\",\n                )\n\n            # 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,","sourceCodeStart":703,"sourceCodeEnd":739,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/model/_base.py#L703-L739","documentation":"_call_api_with_structured_output only accepts structured_model as a dict (JSON schema) or a Pydantic BaseModel subclass. Passing anything else (a class instance, a string, a TypedDict, a dataclass) raises ValueError.","triggerScenarios":"Calling generate_structured_output(msgs, MySchema()) with an instance instead of the class; passing a JSON string of a schema; passing TypedDict/dataclasses/attrs classes.","commonSituations":"Assuming an instantiated model works like OpenAI SDK's parse(); loading schema from JSON file as str; migrating code from pydantic v1 style.","solutions":["Pass the Pydantic class, not an instance: generate_structured_output(msgs, MySchema)","If you have a raw JSON schema, pass it as a dict: json.loads(schema_json)","Convert TypedDict/dataclass schemas to Pydantic BaseModel subclasses"],"exampleFix":"# before\nres = await model.generate_structured_output(msgs, MySchema())\n\n# after\nres = await model.generate_structured_output(msgs, MySchema)","handlingStrategy":"type-guard","validationCode":"from pydantic import BaseModel\nassert isinstance(structured_model, dict) or (isinstance(structured_model, type) and issubclass(structured_model, BaseModel))","typeGuard":"from pydantic import BaseModel\n\ndef is_valid_schema(s) -> bool:\n    return isinstance(s, dict) or (isinstance(s, type) and issubclass(s, BaseModel))","tryCatchPattern":null,"preventionTips":["Always pass the Pydantic class, never an instance","Type-annotate schema parameters as type[BaseModel] | dict in your own wrappers"],"tags":["agentscope","structured-output","pydantic","type-validation"],"backgroundTag":"invalid-schema-type","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}