{"record":{"id":"61bb9cf55ed8c816","repo":"microsoft/autogen","slug":"json-output-must-be-a-boolean-or-a-pydantic-model","errorCode":null,"errorMessage":"json_output must be a boolean or a Pydantic model class, got {type(json_output)}","messagePattern":"json_output must be a boolean or a Pydantic model class, got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/models/ollama/_ollama_client.py","lineNumber":560,"sourceCode":"        if json_output is not None:\n            if self.model_info[\"json_output\"] is False and json_output is True:\n                raise ValueError(\"Model does not support JSON output.\")\n            if json_output is True:\n                # JSON mode.\n                response_format_value = \"json\"\n            elif json_output is False:\n                # Text mode.\n                response_format_value = None\n            elif isinstance(json_output, type) and issubclass(json_output, BaseModel):\n                if response_format_value is not None:\n                    raise ValueError(\n                        \"response_format and json_output cannot be set to a Pydantic model class at the same time. \"\n                        \"Use json_output instead.\"\n                    )\n                # Beta client mode with Pydantic model class.\n                response_format_value = json_output.model_json_schema()\n            else:\n                raise ValueError(f\"json_output must be a boolean or a Pydantic model class, got {type(json_output)}\")\n\n        if \"format\" in create_args:\n            # Handle the case where format is set from create_args.\n            if json_output is not None:\n                raise ValueError(\"json_output and format cannot be set at the same time. Use json_output instead.\")\n            assert response_format_value is None\n            response_format_value = create_args[\"format\"]\n            # Remove format from create_args to prevent passing it twice.\n            del create_args[\"format\"]\n\n        # TODO: allow custom handling.\n        # For now we raise an error if images are present and vision is not supported\n        if self.model_info[\"vision\"] is False:\n            for message in messages:\n                if isinstance(message, UserMessage):\n                    if isinstance(message.content, list) and any(isinstance(x, Image) for x in message.content):\n                        raise ValueError(\"Model does not support vision and image was provided\")\n","sourceCodeStart":542,"sourceCodeEnd":578,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/models/ollama/_ollama_client.py#L542-L578","documentation":"The json_output parameter of OllamaChatCompletionClient.create() must be None, a bool, or a Pydantic BaseModel subclass. The final else in the elif chain raises ValueError echoing the received type. Like error 803, passing a BaseModel *instance* (not the class) is the most frequent trigger, along with strings from config files.","triggerScenarios":"json_output=MyModel() (instance); json_output='True' or 'true' from YAML/JSON config; json_output=1; forwarding a TypedDict or dataclass class.","commonSituations":"Deserialized configuration where booleans arrive as strings; passing a pre-built schema instance from another module; generic wrapper code that forwards arbitrary kwargs into json_output.","solutions":["Pass the class: json_output=MyModel","Coerce config values before the call: parse 'true'/'false' strings to bool","Validate at the boundary: accept only bool | None | type[BaseModel] in your own wrapper's signature"],"exampleFix":"# before\nawait client.create(messages, json_output=SummaryResult())  # instance\n\n# after\nawait client.create(messages, json_output=SummaryResult)  # class","handlingStrategy":"type-guard","validationCode":"def normalize_json_output(v: object) -> bool | type[BaseModel] | None:\n    if v is None or isinstance(v, bool):\n        return v\n    if isinstance(v, type) and issubclass(v, BaseModel):\n        return v\n    if isinstance(v, str) and v.lower() in (\"true\", \"false\"):\n        return v.lower() == \"true\"\n    raise TypeError(f\"unusable json_output: {v!r}\")\n\nresult = await client.create(messages, json_output=normalize_json_output(cfg.get(\"json_output\")))","typeGuard":"def is_valid_ollama_json_output(v: object) -> TypeGuard[bool | type[BaseModel] | None]:\n    return v is None or isinstance(v, bool) or (isinstance(v, type) and issubclass(v, BaseModel))","tryCatchPattern":"try:\n    result = await client.create(messages, json_output=jo)\nexcept ValueError as e:\n    if \"json_output must be a boolean or a Pydantic model class\" in str(e):\n        result = await client.create(messages, json_output=None)\n    else:\n        raise","preventionTips":["Pass BaseModel classes, never instances","Coerce string booleans from config before the call","Type wrapper parameters as Optional[bool | type[BaseModel]] for static checking"],"tags":["ollama","json-output","validation","api-misuse"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}