microsoft/autogen · error · ValueError
Either `json_schema` or `input_model` must be provided.
Error message
Either `json_schema` or `input_model` must be provided.
What it means
StructuredMessageFactory must be given either a JSON schema (json_schema) or a pydantic input model (input_model) to build its ContentModel; it raises ValueError when both are absent. If json_schema is provided it takes precedence via schema_to_pydantic_model; input_model is only used in the elif branch.
Source
Thrown at python/packages/autogen-agentchat/src/autogen_agentchat/messages.py:347
component_type = "structured_message"
def __init__(
self,
json_schema: Optional[Dict[str, Any]] = None,
input_model: Optional[Type[BaseModel]] = None,
format_string: Optional[str] = None,
content_model_name: Optional[str] = None,
) -> None:
self.format_string = format_string
if json_schema:
self.ContentModel = schema_to_pydantic_model(
json_schema, model_name=content_model_name or "GeneratedContentModel"
)
elif input_model:
self.ContentModel = input_model
else:
raise ValueError("Either `json_schema` or `input_model` must be provided.")
self.StructuredMessage = StructuredMessage[self.ContentModel] # type: ignore[name-defined]
def _to_config(self) -> StructureMessageConfig:
return StructureMessageConfig(
json_schema=self.ContentModel.model_json_schema(),
format_string=self.format_string,
content_model_name=self.ContentModel.__name__,
)
@classmethod
def _from_config(cls, config: StructureMessageConfig) -> "StructuredMessageFactory":
return cls(
json_schema=config.json_schema,
format_string=config.format_string,
content_model_name=config.content_model_name,
)
View on GitHub (pinned to 027ecf0a37)
Solutions
- Pass a pydantic model: StructuredMessageFactory(input_model=MyModel)
- Or pass a JSON schema dict: StructuredMessageFactory(json_schema=MyModel.model_json_schema())
- Guard config-driven construction: require at least one of the two keys before calling
- Watch out for falsy values: an empty schema dict {} is treated as absent — pass a real schema object
Example fix
# before
factory = StructuredMessageFactory(format_string="{value}") # ValueError
# after
factory = StructuredMessageFactory(input_model=MyModel, format_string="{value}") Defensive patterns
Strategy: validation
Validate before calling
if json_schema is None and input_model is None:
raise ValueError("Provide json_schema or input_model before constructing the factory")
factory = StructuredMessageFactory(json_schema=json_schema, input_model=input_model) Prevention
- Always pass input_model (preferred for typed code) or a non-empty json_schema dict
- Beware falsy values: json_schema={} is treated as absent
- Validate config payloads require one of the two keys
When it happens
Trigger: StructuredMessageFactory(format_string=...) with neither json_schema nor input_model; both passed as None explicitly (e.g. forwarded kwargs that default to None); building factories from config where the schema key is missing.
Common situations: Refactoring call sites and dropping the schema argument; config-driven construction with optional schema fields left empty; passing a falsy schema (empty dict) — note an empty dict is falsy, so json_schema={} also lands in the error branch.
Related errors
- Handoff name must be a string: {values['name']}
- Unsupported or missing type for field `{key}` in `{model_nam
- Handoff name must be a valid identifier: {values['name']}
- At least one of max_total_token, max_prompt_token, or max_co
- Invalid configuration: {str(e)}
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/092fac2fd4393f55.
Report an issue: GitHub.