{"id":7651,"library":"qianfan","title":"Qianfan Python SDK","description":"The Qianfan Python SDK provides a convenient way to interact with Baidu Wenxin Qianfan Large Model Platform. It supports various AI capabilities including chat completion, text completion, embeddings, text-to-image, and more. The library is actively maintained, with frequent releases across Python, Go, and JavaScript, ensuring up-to-date access to Qianfan services.","status":"active","version":"0.4.12.3","language":"en","source_language":"en","source_url":"https://github.com/baidubce/bce-qianfan-sdk","tags":["AI","LLM","Baidu","Qianfan","SDK","NLP","Generative AI"],"install":[{"cmd":"pip install qianfan","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"Top-level import for core client classes is preferred.","wrong":"from qianfan.chat import ChatCompletion","symbol":"ChatCompletion","correct":"from qianfan import ChatCompletion"},{"symbol":"Completion","correct":"from qianfan import Completion"},{"symbol":"Embedding","correct":"from qianfan import Embedding"},{"note":"Use `QfMessages` for defining chat message roles and content, `Messages` might be deprecated or internal in some contexts.","wrong":"from qianfan import Messages","symbol":"QfMessages","correct":"from qianfan import QfMessages"}],"quickstart":{"code":"import os\nfrom qianfan import ChatCompletion, QfMessages\n\n# Set your Baidu Cloud API Key and Secret Key as environment variables\n# or pass them directly to the client constructor.\n# os.environ[\"QIANFAN_AK\"] = \"YOUR_AK\"\n# os.environ[\"QIANFAN_SK\"] = \"YOUR_SK\"\n\nak = os.environ.get(\"QIANFAN_AK\", \"\")\nsk = os.environ.get(\"QIANFAN_SK\", \"\")\n\nif not ak or not sk:\n    print(\"Please set QIANFAN_AK and QIANFAN_SK environment variables.\")\nelse:\n    chat_comp = ChatCompletion(ak=ak, sk=sk)\n\n    messages = [\n        QfMessages(role=\"user\", content=\"Hello, how are you?\")\n    ]\n\n    try:\n        response = chat_comp.do(\n            model=\"ERNIE-Bot-4\", # Or another available chat model like 'ERNIE-Bot-turbo'\n            messages=messages,\n            stream=False\n        )\n\n        if response and response.result:\n            print(f\"Qianfan response: {response.result}\")\n        else:\n            print(\"No valid response from Qianfan.\")\n\n    except Exception as e:\n        print(f\"An error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to perform a simple chat completion using the `qianfan` SDK. It initializes a `ChatCompletion` client, constructs messages using `QfMessages`, and sends a request to the ERNIE-Bot-4 model. Ensure your `QIANFAN_AK` and `QIANFAN_SK` environment variables are set for authentication."},"warnings":[{"fix":"Set `QIANFAN_AK` and `QIANFAN_SK` in your environment, or initialize clients like `ChatCompletion(ak=\"YOUR_AK\", sk=\"YOUR_SK\")`.","message":"Authentication requires 'QIANFAN_AK' and 'QIANFAN_SK' environment variables. If these are not set, the SDK client must be initialized by passing `ak` and `sk` keyword arguments directly, otherwise API calls will fail.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use the official, case-sensitive model names provided by Qianfan, e.g., `model=\"ERNIE-Bot-4\"`.","message":"As of v0.4.12.3, the SDK removed the forced lowercase conversion of model names. This means you must now use the exact case-sensitive model names as specified in the Qianfan documentation (e.g., 'ERNIE-Bot-4' instead of 'ernie-bot-4').","severity":"breaking","affected_versions":">=0.4.12.3"},{"fix":"Process streaming responses with a loop: `for chunk in chat_comp.do(..., stream=True): print(chunk.result)`.","message":"When using streaming (`stream=True`), the `do()` method returns an iterable. You must iterate over the response to receive partial results. Not iterating will result in no output.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Translate error messages or consult the Baidu Qianfan API documentation for known error codes and their English descriptions.","message":"Some error messages from the Qianfan API might be returned in Chinese, which can be challenging for non-Chinese speakers to debug directly. Refer to the official documentation for common error codes.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Verify that `QIANFAN_AK` and `QIANFAN_SK` environment variables are correctly set and valid, or explicitly pass `ak` and `sk` to the client constructor, e.g., `ChatCompletion(ak='YOUR_AK', sk='YOUR_SK')`.","cause":"The `QIANFAN_AK` or `QIANFAN_SK` environment variables are missing, incorrect, or not loaded, or `ak`/`sk` were not passed during client initialization.","error":"qianfan.common.exception.APIErrorCode: ErrorCode=2, ErrorMsg=Authentication failed: The Access Key or Secret Key is invalid."},{"fix":"Ensure `qianfan` is installed (`pip install qianfan`) and up-to-date (`pip install --upgrade qianfan`). Verify the import statement for typos, it should be `from qianfan import ChatCompletion`.","cause":"This usually means the `qianfan` package is not installed, or you are running an outdated version that doesn't expose `ChatCompletion` at the top level, or there's a typo in the import.","error":"ImportError: cannot import name 'ChatCompletion' from 'qianfan'"},{"fix":"Check the official Qianfan documentation for the exact, case-sensitive names of available models. Ensure the model is enabled for your account. For versions >=0.4.12.3, pay close attention to model name casing.","cause":"The specified model name is either incorrect, misspelled, not supported by your account, or uses incorrect casing (e.g., 'ernie-bot' instead of 'ERNIE-Bot').","error":"qianfan.common.exception.APIErrorCode: ErrorCode=17, ErrorMsg=The provided model name is invalid."},{"fix":"Remove the `stream=...` parameter if the API endpoint does not support streaming (e.g., embedding generation). For chat/completion APIs that do support streaming, ensure your `qianfan` SDK version is up-to-date and the `stream` parameter is used correctly for that specific model.","cause":"You might be calling a method (e.g., for embeddings or non-streaming completion) that does not support the `stream` parameter, or using an older client version that doesn't support streaming for that specific API.","error":"TypeError: do() got an unexpected keyword argument 'stream'"}]}