AIStudio Python SDK
raw JSON → 0.3.8 verified Sun Apr 12 auth: no python
The `aistudio-sdk` is a Python client library for interacting with the AIStudio API. It provides convenient access to AI models for tasks like text generation. Currently at version 0.3.8, it receives frequent updates aligning with the underlying API's evolution.
pip install aistudio-sdk Common errors
error ImportError: cannot import name 'AistudioClient' from 'aistudio_sdk' ↓
cause The 'AistudioClient' class was moved to a different module in aistudio_sdk version 0.3.0.
fix
from aistudio_sdk.client import AistudioClient
error ModuleNotFoundError: No module named 'aistudio_sdk' ↓
cause The aistudio_sdk package is not installed in the current Python environment.
fix
pip install aistudio-sdk
error ModuleNotFoundError: No module named 'paddle.fluid.dataloader.collate' ↓
cause The 'paddle.fluid.dataloader.collate' module was removed or relocated in recent versions of PaddlePaddle.
fix
Update the import statement to the correct module path or install a compatible version of PaddlePaddle.
error KeyError: 'GOOGLE_API_KEY' ↓
cause The environment variable for the Google AI Studio API key (e.g., `GOOGLE_API_KEY`) is not set or is misspelled, preventing the SDK from authenticating with the API.
fix
Set the API key as an environment variable before running your application (e.g.,
export GOOGLE_API_KEY='your_api_key_here' in your terminal) or pass it directly when initializing the client, ensuring the key name matches the one expected by the SDK. error AttributeError: 'GenerativeModel' object has no attribute 'generate_embeddings' ↓
cause This error often indicates a version mismatch or breaking change in the underlying `google-generativeai` library or the AI Studio API it wraps, where the `generate_embeddings` method may have been moved, renamed, or is not available for the specific model or SDK version being used.
fix
Ensure you are using a compatible version of
google-generativeai and aistudio-sdk. Upgrade both packages to their latest stable versions (pip install --upgrade google-generativeai aistudio-sdk) and refer to the official documentation for the correct method calls for embedding generation for your chosen model. Warnings
breaking As the library is in its 0.x.x version, breaking changes to the API surface (class names, method signatures, return types) may occur in minor releases without a full major version bump. ↓
fix Always review release notes for `aistudio-sdk` when upgrading. Pin your dependency to a specific minor version (e.g., `aistudio-sdk~=0.3.0`) in production to control updates.
gotcha The SDK primarily relies on the `AISTUDIO_API_KEY` environment variable for authentication. While a `Client` object can be instantiated with an `api_key` argument, service classes like `TextGeneration` do not directly accept `api_key` in their constructor. ↓
fix Ensure `AISTUDIO_API_KEY` is consistently set in your environment. If you need to manage multiple keys or explicitly pass one, instantiate `aistudio.Client(api_key="...")` first and then pass that client to service classes if they support it, or set the environment variable programmatically with `os.environ['AISTUDIO_API_KEY'] = '...'` before initializing the service.
gotcha The example models provided in the documentation (e.g., 'gpt-3.5-turbo') are illustrative. The actual availability and names of models depend on the AIStudio API itself and may change. Using an unsupported model will result in an API error. ↓
fix Consult the official AIStudio API documentation or SDK methods (if available) to get a list of currently supported models before making API calls.
gotcha API responses may contain detailed error messages or specific structures beyond just `response.text` for generation results. Ignoring these can lead to missed debugging opportunities. ↓
fix When an API call fails, inspect the full exception object or response object for `status_code`, `error` fields, or other diagnostic information to understand the cause (e.g., rate limits, invalid input, authentication issues).
Imports
- TextGeneration
from aistudio import TextGeneration - Client wrong
from aistudio.client import Clientcorrectfrom aistudio import Client
Quickstart
import os
from aistudio import TextGeneration
# Ensure the AISTUDIO_API_KEY environment variable is set.
# Example: export AISTUDIO_API_KEY="your_secret_key"
# For local testing without setting a real key (will likely fail API calls):
# os.environ['AISTUDIO_API_KEY'] = 'sk-aistudio-dummy-key'
try:
# The SDK automatically picks up AISTUDIO_API_KEY from environment variables.
gen = TextGeneration()
response = gen.create(
prompt="Hello, my name is",
model="gpt-3.5-turbo", # Replace with an available model on AIStudio
temperature=0.7,
max_tokens=20
)
print("Generated text:", response.text)
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure AISTUDIO_API_KEY is set and valid, and the model specified is available.")