agentscope-ai/agentscope · error · ValueError
The structured_model is expected to be a subclass of Pydanti
Error message
The structured_model is expected to be a subclass of Pydantic.BaseModel or a dict, but got {type(structured_model)}. What it means
_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.
Source
Thrown at src/agentscope/model/_base.py:721
_.input,
input_schema,
)
break
if structured_output is None:
raise StructuredOutputError(
"Failed to generate structured output for model.",
)
# Validate the output
if isinstance(structured_model, dict):
jsonschema.validate(structured_output, structured_model)
elif issubclass(structured_model, BaseModel):
structured_model.model_validate(structured_output)
else:
raise ValueError(
"The structured_model is expected to be a subclass of "
"Pydantic.BaseModel or a dict, "
f"but got {type(structured_model)}.",
)
except (
ToolJSONDecodeError,
jsonschema.ValidationError,
PydanticValidationError,
) as e:
raise StructuredOutputError(
f"Invalid structured output from model {model_name}: {e}",
) from e
return StructuredResponse(
id=completed_response.id,
created_at=completed_response.created_at,
content=structured_output,
usage=completed_response.usage,View on GitHub (pinned to e90f1c7592)
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
Example fix
# before res = await model.generate_structured_output(msgs, MySchema()) # after res = await model.generate_structured_output(msgs, MySchema)
Defensive patterns
Strategy: type-guard
Validate before calling
from pydantic import BaseModel assert isinstance(structured_model, dict) or (isinstance(structured_model, type) and issubclass(structured_model, BaseModel))
Type guard
from pydantic import BaseModel
def is_valid_schema(s) -> bool:
return isinstance(s, dict) or (isinstance(s, type) and issubclass(s, BaseModel)) Prevention
- Always pass the Pydantic class, never an instance
- Type-annotate schema parameters as type[BaseModel] | dict in your own wrappers
When it happens
Trigger: 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.
Common situations: Assuming an instantiated model works like OpenAI SDK's parse(); loading schema from JSON file as str; migrating code from pydantic v1 style.
Related errors
- Invalid structured output from model {model_name}: {e}
- The injection template must contain the '{runtime_state}' pl
- The input messages cannot be empty for the `generate_structu
- No structured-output strategy is available for {self.model}.
- Failed to get the completed response from model {model_name}
AI-assisted analysis of agentscope-ai/agentscope@e90f1c7592 (2026-08-28).
Data as JSON: /api/errors/5d797ca4780e5638.
Report an issue: GitHub.