BerriAI/litellm · error · ValueError

Error calling litellm.completion for generate_content: {e}

Error message

Error calling litellm.completion for generate_content: {e}

What it means

Catch-all from the sync generate_content adapter: every exception thrown by the underlying litellm.completion call (and the streaming-transform raise above it) is caught and re-raised as ValueError('Error calling litellm.completion for generate_content: <original>'). Diagnose the appended original message, not the wrapper.

Source

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

                    )
                    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.completion for generate_content: {e}")

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Inspect the suffix of the message / full traceback to find the true underlying error and fix that
  2. Verify the model string and that the required API key env var is set for the resolved provider
  3. Simplify: reproduce with litellm.completion(model='gemini/<model>', messages=[...]) to see the raw provider error
  4. Update litellm if the failure is inside the transform itself

Example fix

# before
except ValueError as e:
    log(str(e))

# after
import traceback
except ValueError:
    traceback.print_exc()  # original exception is chained
    raise
Defensive patterns

Strategy: try-catch

Try / catch

try:
    r = generate_content(model=m, contents=c)
except ValueError as e:
    # suffix after 'generate_content: ' is the real provider error
    root = str(e).rsplit(": ", 1)[-1]
    handle(root)

Prevention

When it happens

Trigger: Sync generate_content calls that fail inside litellm.completion: bad/missing API keys, contents that cannot map to OpenAI messages, unknown model→provider mapping, or the streaming transform ValueError from the same try block.

Common situations: Switching from the official google-genai SDK to litellm's adapter and carrying over unsupported kwargs; API key env var naming mismatches (GEMINI_API_KEY vs GOOGLE_API_KEY); stale model names.

Related errors


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