harry0703/MoneyPrinterTurbo · error · Exception

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

Error message

[{llm_provider}] returned an invalid response: "{response}", please check your network connection and try again.

What it means

Raised in the azure branch when AzureOpenAI's chat.completions.create() returns a truthy object that is not an openai.types.chat.ChatCompletion. The Azure OpenAI SDK should raise APIError subclasses on failure, so a non-ChatCompletion return indicates a broken SDK installation, a mocked client in tests, or a custom base_url serving non-standard responses.

Source

Thrown at app/services/llm.py:347

        if adapter == "azure":
            # Azure OpenAI SDK 使用 `azure_endpoint` 和 `api_version` 生成专用请求地址,
            # 不能继续复用下面普通 OpenAI-compatible 的 `base_url` 初始化逻辑。
            # 这里在 Azure 分支内完成请求并立即返回,避免客户端被后续 fallback
            # 覆盖,导致用户配置的 Azure 凭证通过校验但实际请求没有被使用。
            logger.info(f"requesting azure chat completion, model: {model_name}")
            client = AzureOpenAI(
                api_key=api_key,
                api_version=api_version,
                azure_endpoint=base_url,
            )
            response = client.chat.completions.create(
                model=model_name, messages=[{"role": "user", "content": prompt}]
            )
            if response:
                if isinstance(response, ChatCompletion):
                    return _extract_chat_completion_text(response, llm_provider)
                else:
                    raise Exception(
                        f'[{llm_provider}] returned an invalid response: "{response}", please check your network '
                        f"connection and try again."
                    )
            else:
                raise Exception(
                    f"[{llm_provider}] returned an empty response, please check your network connection and try again."
                )

        if adapter == "modelscope":
            content = ""
            client = OpenAI(
                api_key=api_key,
                base_url=base_url,
            )
            response = client.chat.completions.create(
                model=model_name,
                messages=[{"role": "user", "content": prompt}],
                extra_body={"enable_thinking": False},

View on GitHub (pinned to 1f9f19c202)

Solutions

  1. Reinstall the openai package cleanly: pip install --force-reinstall openai==<pinned version from requirements]
  2. Verify [azure].base_url is the azure_endpoint form (https://<resource>.openai.azure.com/) and api_version matches your resource's supported versions
  3. Print type(response) in a debug run to identify what is actually returned
  4. In tests, mock create() to return a real ChatCompletion (construct one via openai.types.chat.ChatCompletion.model_construct)
Defensive patterns

Strategy: type-guard

Type guard

from openai.types.chat import ChatCompletion
def is_chat_completion(resp) -> bool:
    return isinstance(resp, ChatCompletion)

Try / catch

except Exception: — non-retryable type-contract failure; fix SDK pinning or endpoint, then retry manually

Prevention

When it happens

Trigger: client.chat.completions.create(...) via AzureOpenAI returning e.g. a dict or stripped object — mismatched openai package versions (AzureOpenAI client from one version deserializing into types from another), test fakes returning SimpleNamespace, or a proxy at azure_endpoint returning JSON the SDK cannot type but does not reject.

Common situations: Mixed openai package versions after partial upgrades (openai + openai-types confusion); patched/mocked AzureOpenAI in tests returning wrong types; azure_endpoint pointing at an API-management gateway that alters response schemas; very old openai SDK versions lacking proper response typing.

Related errors


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