microsoft/autogen · error · ValueError

include_usage and extra_create_args['stream_options']['inclu

Error message

include_usage and extra_create_args['stream_options']['include_usage'] are both set, but differ in value.

What it means

Thrown during streaming (create_stream) when the explicit include_usage parameter and extra_create_args['stream_options']['include_usage'] are both set but to different values. The client refuses to guess which the caller intended, since usage accounting would silently differ from the request options.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py:862

            - `max_tokens` (int): The maximum number of tokens to generate in the completion.
            - `top_p` (float): An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass.
            - `frequency_penalty` (float): A value between -2.0 and 2.0 that penalizes new tokens based on their existing frequency in the text so far, decreasing the likelihood of repeated phrases.
            - `presence_penalty` (float): A value between -2.0 and 2.0 that penalizes new tokens based on whether they appear in the text so far, encouraging the model to talk about new topics.
        """

        create_params = self._process_create_args(
            messages,
            tools,
            tool_choice,
            json_output,
            extra_create_args,
        )

        if include_usage is not None:
            if "stream_options" in create_params.create_args:
                stream_options = create_params.create_args["stream_options"]
                if "include_usage" in stream_options and stream_options["include_usage"] != include_usage:
                    raise ValueError(
                        "include_usage and extra_create_args['stream_options']['include_usage'] are both set, but differ in value."
                    )
            else:
                # If stream options are not present, add them.
                create_params.create_args["stream_options"] = {"include_usage": True}

        if max_consecutive_empty_chunk_tolerance != 0:
            warnings.warn(
                "The 'max_consecutive_empty_chunk_tolerance' parameter is deprecated and will be removed in the future releases. All of empty chunks will be skipped with a warning.",
                DeprecationWarning,
                stacklevel=2,
            )

        if create_params.response_format is not None:
            chunks = self._create_stream_chunks_beta_client(
                tool_params=create_params.tools,
                oai_messages=create_params.messages,
                response_format=create_params.response_format,

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Set the value in only one place: either the include_usage parameter or extra_create_args['stream_options']['include_usage'], not both
  2. If both must appear, make them equal (both True or both False)
  3. Remove 'stream_options' from extra_create_args and rely on the include_usage parameter

Example fix

# before
async for chunk in client.create_stream(
    [msg],
    include_usage=False,
    extra_create_args={"stream_options": {"include_usage": True}},
):
    ...

# after
async for chunk in client.create_stream(
    [msg],
    include_usage=True,
):
    ...
Defensive patterns

Strategy: validation

Validate before calling

extra = dict(extra_create_args)
stream_opts = extra.get("stream_options", {})
if "include_usage" in stream_opts:
    stream_opts["include_usage"] = bool(stream_opts["include_usage"])
    include_usage = stream_opts["include_usage"]  # single source of truth
extra["stream_options"] = stream_opts

Try / catch

try:
    async for chunk in client.create_stream(messages, include_usage=iu, extra_create_args=extra):
        ...
except ValueError as e:
    if "include_usage" in str(e):
        extra.pop("stream_options", None)  # retry with only the parameter
        async for chunk in client.create_stream(messages, include_usage=iu):
            ...
    else:
        raise

Prevention

When it happens

Trigger: Calling create_stream with include_usage=False while also passing extra_create_args={'stream_options': {'include_usage': True}} (or the reverse mismatch). Only differing values raise; equal values are accepted.

Common situations: Copy-pasting request kwargs (with stream_options) into a call that also sets the dedicated include_usage argument; libraries wrapping create_stream that add stream_options for their own usage tracking while the app sets include_usage separately.

Related errors


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