{"record":{"id":"092fac2fd4393f55","repo":"microsoft/autogen","slug":"either-json-schema-or-input-model-must-be-prov","errorCode":null,"errorMessage":"Either `json_schema` or `input_model` must be provided.","messagePattern":"Either `json_schema` or `input_model` must be provided\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-agentchat/src/autogen_agentchat/messages.py","lineNumber":347,"sourceCode":"    component_type = \"structured_message\"\n\n    def __init__(\n        self,\n        json_schema: Optional[Dict[str, Any]] = None,\n        input_model: Optional[Type[BaseModel]] = None,\n        format_string: Optional[str] = None,\n        content_model_name: Optional[str] = None,\n    ) -> None:\n        self.format_string = format_string\n\n        if json_schema:\n            self.ContentModel = schema_to_pydantic_model(\n                json_schema, model_name=content_model_name or \"GeneratedContentModel\"\n            )\n        elif input_model:\n            self.ContentModel = input_model\n        else:\n            raise ValueError(\"Either `json_schema` or `input_model` must be provided.\")\n\n        self.StructuredMessage = StructuredMessage[self.ContentModel]  # type: ignore[name-defined]\n\n    def _to_config(self) -> StructureMessageConfig:\n        return StructureMessageConfig(\n            json_schema=self.ContentModel.model_json_schema(),\n            format_string=self.format_string,\n            content_model_name=self.ContentModel.__name__,\n        )\n\n    @classmethod\n    def _from_config(cls, config: StructureMessageConfig) -> \"StructuredMessageFactory\":\n        return cls(\n            json_schema=config.json_schema,\n            format_string=config.format_string,\n            content_model_name=config.content_model_name,\n        )\n","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-agentchat/src/autogen_agentchat/messages.py#L329-L365","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nfactory = StructuredMessageFactory(format_string=\"{value}\")  # ValueError\n\n# after\nfactory = StructuredMessageFactory(input_model=MyModel, format_string=\"{value}\")","handlingStrategy":"validation","validationCode":"if json_schema is None and input_model is None:\n    raise ValueError(\"Provide json_schema or input_model before constructing the factory\")\nfactory = StructuredMessageFactory(json_schema=json_schema, input_model=input_model)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["structured-message","json-schema","pydantic","validation","agentchat"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}