BerriAI/litellm · error · ValueError

Failed to transform streaming response

Error message

Failed to transform streaming response

What it means

In the async Google GenAI generate_content adapter, a streaming completion result is converted chunk-by-chunk via translate_completion_output_params_streaming. That helper returns None when a chunk cannot be transformed (e.g. a CustomStreamWrapper or chunk shape it does not recognize); the adapter treats None as failure and raises this ValueError.

Source

Thrown at litellm/google_genai/adapters/handler.py:93

            completion_response: Final = await litellm.acompletion(**completion_kwargs)

            if stream:
                # Check if completion_response is actually a stream or a ModelResponse
                # This can happen in error cases or when stream is not properly supported
                if not hasattr(completion_response, "__aiter__"):
                    # If it's not a stream, treat it as a regular response
                    generate_content_response = GOOGLE_GENAI_ADAPTER.translate_completion_to_generate_content(
                        cast(ModelResponse, completion_response)
                    )
                    return generate_content_response
                else:
                    # Transform streaming completion response to generate_content format
                    transformed_stream: Final = GOOGLE_GENAI_ADAPTER.translate_completion_output_params_streaming(
                        completion_response
                    )
                    if transformed_stream is not None:
                        return transformed_stream
                    raise ValueError("Failed to transform streaming response")
            else:
                # Transform completion response back to generate_content format
                generate_content_response = GOOGLE_GENAI_ADAPTER.translate_completion_to_generate_content(
                    cast(ModelResponse, completion_response)
                )
                return generate_content_response

        except Exception as e:
            raise ValueError(f"Error calling litellm.acompletion for generate_content: {e}")

    @staticmethod
    def generate_content_handler(
        model: str,
        contents: list[dict[str, object]] | dict[str, object],
        litellm_params: GenericLiteLLMParams,
        config: dict[str, object] | None = None,
        stream: bool = False,
        _is_async: bool = False,

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Update litellm — the streaming translator gains coverage for new chunk shapes over releases
  2. Retry with stream=False to confirm the non-streaming transform path works for the same model/params
  3. Call litellm.acompletion directly with the equivalent model (e.g. 'gemini/gemini-2.5-flash') instead of the generate_content adapter if you don't need the Google-native response shape
  4. Report the chunk type that fails: log type(completion_response) and its first chunk to help pinpoint the unhandled case

Example fix

# before
resp = await agenerate_content(model='gemini-2.5-flash', contents=[...], stream=True)

# after
resp = await agenerate_content(model='gemini-2.5-flash', contents=[...], stream=False)
# or bypass the adapter:
resp = await litellm.acompletion(model='gemini/gemini-2.5-flash', messages=[...], stream=True)
Defensive patterns

Strategy: fallback

Try / catch

try:
    stream = await agenerate_content(model=m, contents=c, stream=True)
except ValueError as e:
    if "Failed to transform streaming response" in str(e):
        stream = None
        result = await agenerate_content(model=m, contents=c, stream=False)  # fallback

Prevention

When it happens

Trigger: Calling the adapter-backed acompletion path with stream=True where the completion response object is not a recognized streaming chunk type — for example a provider config was not found so the adapter path ran, and the returned wrapper's first yielded object fails the streaming transform.

Common situations: Using litellm.google_genai generate_content passthrough with stream=True for models/providers the adapter doesn't fully map; LiteLLM version drift where new chunk types (e.g. usage-only chunks, thinking chunks) aren't handled by the translator.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/71a7e1ebd1773ad0. Report an issue: GitHub.