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

  1. Pass a pydantic model: StructuredMessageFactory(input_model=MyModel)
  2. Or pass a JSON schema dict: StructuredMessageFactory(json_schema=MyModel.model_json_schema())
  3. Guard config-driven construction: require at least one of the two keys before calling
  4. 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

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


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/092fac2fd4393f55. Report an issue: GitHub.