harry0703/MoneyPrinterTurbo · error · Exception

[{llm_provider}] returned an invalid response: "{response}"

Error message

[{llm_provider}] returned an invalid response: "{response}"

What it means

Raised in the qwen branch when dashscope.Generation.call() returns a truthy object that is not a dashscope GenerationResponse instance. This is a type-contract failure between the installed dashscope SDK and this code, not a server-side error — the isinstance(response, GenerationResponse) guard tripped.

Source

Thrown at app/services/llm.py:234

        if adapter == "qwen":
            import dashscope
            from dashscope.api_entities.dashscope_response import GenerationResponse

            dashscope.api_key = api_key
            response = dashscope.Generation.call(
                model=model_name, messages=[{"role": "user", "content": prompt}]
            )
            if response:
                if isinstance(response, GenerationResponse):
                    status_code = response.status_code
                    if status_code != 200:
                        raise Exception(
                            f'[{llm_provider}] returned an error response: "{response}"'
                        )

                    return _extract_qwen_generation_text(response)
                else:
                    raise Exception(
                        f'[{llm_provider}] returned an invalid response: "{response}"'
                    )
            else:
                raise Exception(f"[{llm_provider}] returned an empty response")

        if adapter == "gemini":
            from google import genai
            from google.genai import types

            http_options = types.HttpOptions(base_url=base_url) if base_url else None
            generation_config = types.GenerateContentConfig(
                temperature=0.5,
                top_p=1,
                top_k=1,
                max_output_tokens=2048,
                safety_settings=[
                    types.SafetySetting(
                        category="HARM_CATEGORY_HARASSMENT",

View on GitHub (pinned to 1f9f19c202)

Solutions

  1. Pin dashscope to a version known to work with this codebase (check requirements and lockfile) and reinstall: pip install dashscope==<known-good>
  2. Print type(response) and response.__class__.__module__ in a debug run to confirm which class is actually returned
  3. If a newer dashscope intentionally changed the return type, update the isinstance check in app/services/llm.py to use duck-typing (check status_code attribute presence) or the new response class
  4. In tests, patch dashscope.Generation.call to return a real GenerationResponse instance, not a plain object

Example fix

# before
if isinstance(response, GenerationResponse):
    ...
else:
    raise Exception(f'[{llm_provider}] returned an invalid response: "{response}"')

# after (duck-type on the fields actually used)
if isinstance(response, GenerationResponse) or hasattr(response, "status_code"):
    ...
else:
    raise TypeError(
        f"[qwen] unexpected dashscope response type: {type(response).__name__}; "
        "check dashscope SDK version"
    )
Defensive patterns

Strategy: type-guard

Type guard

def is_dashscope_generation_response(resp) -> bool:
    from dashscope.api_entities.dashscope_response import GenerationResponse
    return isinstance(resp, GenerationResponse) or (
        hasattr(resp, "status_code") and hasattr(resp, "output")
    )

Try / catch

except Exception: — treat as a non-retryable SDK-compatibility failure; report dashscope version and switch provider or pin the SDK rather than retrying

Prevention

When it happens

Trigger: A dashscope SDK version whose Generation.call returns a different response class (or a dict-like wrapper), a mocked/stubbed dashscope in tests, or a partially imported dashscope where GenerationResponse comes from a different module path than the returned object's class.

Common situations: Upgrading or downgrading the dashscope package so api_entities.dashscope_response.GenerationResponse no longer matches the actual return type; test environments with fake dashscope modules; SDK refactor in newer dashscope releases (e.g. new streaming/async APIs returning different types).

Related errors


AI-assisted analysis of harry0703/MoneyPrinterTurbo@1f9f19c202 (2026-08-14). Data as JSON: /api/errors/ecb0a52c3cfff1f1. Report an issue: GitHub.