{"record":{"id":"ce6022c191bc45b1","repo":"harry0703/MoneyPrinterTurbo","slug":"llm-provider-returned-an-invalid-response-r-ce6022","errorCode":null,"errorMessage":"[{llm_provider}] returned an invalid response: \"{response}\", please check your network connection and try again.","messagePattern":"\\[(.+?)\\] returned an invalid response: \"(.+?)\", please check your network connection and try again\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"app/services/llm.py","lineNumber":347,"sourceCode":"        if adapter == \"azure\":\n            # Azure OpenAI SDK 使用 `azure_endpoint` 和 `api_version` 生成专用请求地址，\n            # 不能继续复用下面普通 OpenAI-compatible 的 `base_url` 初始化逻辑。\n            # 这里在 Azure 分支内完成请求并立即返回，避免客户端被后续 fallback\n            # 覆盖，导致用户配置的 Azure 凭证通过校验但实际请求没有被使用。\n            logger.info(f\"requesting azure chat completion, model: {model_name}\")\n            client = AzureOpenAI(\n                api_key=api_key,\n                api_version=api_version,\n                azure_endpoint=base_url,\n            )\n            response = client.chat.completions.create(\n                model=model_name, messages=[{\"role\": \"user\", \"content\": prompt}]\n            )\n            if response:\n                if isinstance(response, ChatCompletion):\n                    return _extract_chat_completion_text(response, llm_provider)\n                else:\n                    raise Exception(\n                        f'[{llm_provider}] returned an invalid response: \"{response}\", please check your network '\n                        f\"connection and try again.\"\n                    )\n            else:\n                raise Exception(\n                    f\"[{llm_provider}] returned an empty response, please check your network connection and try again.\"\n                )\n\n        if adapter == \"modelscope\":\n            content = \"\"\n            client = OpenAI(\n                api_key=api_key,\n                base_url=base_url,\n            )\n            response = client.chat.completions.create(\n                model=model_name,\n                messages=[{\"role\": \"user\", \"content\": prompt}],\n                extra_body={\"enable_thinking\": False},","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/harry0703/MoneyPrinterTurbo/blob/1f9f19c2021a68d04df228f33e9099a0c947f6f8/app/services/llm.py#L329-L365","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Reinstall the openai package cleanly: pip install --force-reinstall openai==<pinned version from requirements]","Verify [azure].base_url is the azure_endpoint form (https://<resource>.openai.azure.com/) and api_version matches your resource's supported versions","Print type(response) in a debug run to identify what is actually returned","In tests, mock create() to return a real ChatCompletion (construct one via openai.types.chat.ChatCompletion.model_construct)"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"from openai.types.chat import ChatCompletion\ndef is_chat_completion(resp) -> bool:\n    return isinstance(resp, ChatCompletion)","tryCatchPattern":"except Exception: — non-retryable type-contract failure; fix SDK pinning or endpoint, then retry manually","preventionTips":["Pin the openai package version; never mix client and types from different versions","Use the deployment name (not model name) as model_name for Azure","Mock AzureOpenAI.create with real ChatCompletion objects in tests"],"tags":["llm","azure","openai-sdk","version-compatibility","type-error"],"backgroundTag":null,"analyzedSha":"1f9f19c2021a68d04df228f33e9099a0c947f6f8","analyzedAt":"2026-08-14T19:41:05.568Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}