microsoft/autogen · error · ValueError

response_format and json_output cannot be set to a Pydantic

Error message

response_format and json_output cannot be set to a Pydantic model class at the same time. Use json_output instead.

What it means

create() rejects specifying a Pydantic schema through both channels at once: a BaseModel class in create_args['response_format'] (deprecated path, which sets response_format_value) and another BaseModel class as json_output. Because response_format_value is not None when json_output is a BaseModel subclass, this ValueError fires telling you to use json_output only.

Source

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

            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()
            else:
                raise ValueError(f"json_output must be a boolean or a Pydantic model class, got {type(json_output)}")

        if "format" in create_args:
            # Handle the case where format is set from create_args.
            if json_output is not None:
                raise ValueError("json_output and format cannot be set at the same time. Use json_output instead.")
            assert response_format_value is None
            response_format_value = create_args["format"]
            # Remove format from create_args to prevent passing it twice.
            del create_args["format"]

        # TODO: allow custom handling.

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Keep only json_output=MyModel and remove response_format from extra_create_args
  2. Sanitize merged configs: if both keys hold Pydantic classes, drop response_format
  3. Prefer json_output everywhere — response_format in this client already emits DeprecationWarning

Example fix

# before
await client.create(messages, extra_create_args={"response_format": StepPlan}, json_output=StepPlan)

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

Strategy: validation

Validate before calling

if "response_format" in extra_create_args and json_output is not None:
    extra_create_args = {k: v for k, v in extra_create_args.items() if k != "response_format"}
result = await client.create(messages, extra_create_args=extra_create_args, json_output=json_output)

Try / catch

try:
    result = await client.create(messages, extra_create_args=extra, json_output=jo)
except ValueError as e:
    if "cannot be set to a Pydantic model class at the same time" in str(e):
        extra.pop("response_format", None)
        result = await client.create(messages, extra_create_args=extra, json_output=jo)
    else:
        raise

Prevention

When it happens

Trigger: create(messages, extra_create_args={'response_format': MyModel}, json_output=MyModel); config templating that sets both a legacy response_format and the new json_output schema.

Common situations: Half-finished migration from response_format to json_output leaving both keys in a merged config; shared request-builder code appending defaults for both parameters.

Related errors


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