microsoft/autogen · error · ValueError

response_format must be a Pydantic model class, not {type(va

Error message

response_format must be a Pydantic model class, not {type(value)}

What it means

Inside create(), if create_args carries a response_format (deprecated path), its value must be a Pydantic BaseModel subclass — the client takes value.model_json_schema() from it. Anything else (a string, dict, instance, or non-Pydantic class) raises this ValueError naming the actual type received.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/models/ollama/_ollama_client.py:540

        create_args = self._create_args.copy()
        create_args.update(extra_create_args)
        create_args = _create_args_from_config(create_args)

        response_format_value: JsonSchemaValue | Literal["json"] | None = None

        if "response_format" in create_args:
            warnings.warn(
                "Using response_format will be deprecated. Use json_output instead.",
                DeprecationWarning,
                stacklevel=2,
            )
            value = create_args["response_format"]
            if isinstance(value, type) and issubclass(value, BaseModel):
                response_format_value = value.model_json_schema()
                # Remove response_format from create_args to prevent passing it twice.
                del create_args["response_format"]
            else:
                raise ValueError(f"response_format must be a Pydantic model class, not {type(value)}")

        if json_output is not None:
            if self.model_info["json_output"] is False and json_output is True:
                raise ValueError("Model does not support JSON output.")
            if json_output is True:
                # JSON mode.
                response_format_value = "json"
            elif json_output is False:
                # Text mode.
                response_format_value = None
            elif isinstance(json_output, type) and issubclass(json_output, BaseModel):
                if response_format_value is not None:
                    raise ValueError(
                        "response_format and json_output cannot be set to a Pydantic model class at the same time. "
                        "Use json_output instead."
                    )
                # Beta client mode with Pydantic model class.
                response_format_value = json_output.model_json_schema()

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Use the supported parameter instead: json_output=True for JSON mode, or json_output=MyModel for schema-driven output
  2. If you must use response_format, pass the Pydantic class itself: response_format=MyModel
  3. Strip response_format from forwarded kwargs dicts before calling create()

Example fix

# before
result = await client.create(messages, extra_create_args={"response_format": {"type": "json_object"}})

# after
result = await client.create(messages, json_output=True)
Defensive patterns

Strategy: type-guard

Validate before calling

extra_create_args.pop("response_format", None)  # use json_output instead
result = await client.create(messages, json_output=True)

Type guard

def is_pydantic_class(v: object) -> TypeGuard[type[BaseModel]]:
    return isinstance(v, type) and issubclass(v, BaseModel)

Try / catch

try:
    result = await client.create(messages, extra_create_args=extra)
except ValueError as e:
    if "response_format must be a Pydantic model class" in str(e):
        extra.pop("response_format", None)
        result = await client.create(messages, extra_create_args=extra, json_output=True)
    else:
        raise

Prevention

When it happens

Trigger: create(messages, extra_create_args={'response_format': {'type': 'json_object'}}) — the OpenAI-style dict is not accepted here; response_format='json'; response_format=MyModel() (instance).

Common situations: Copying OpenAI client kwargs into extra_create_args for Ollama; older autogen examples that used Pydantic-class response_format; migrating from a 0.2-style config dict.

Related errors


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