SAP AI SDK for Generative AI
The SAP Cloud SDK for AI (Python) is designed to simplify interactions with generative AI APIs and services, particularly within the SAP ecosystem. It provides unified interfaces for various large language models (LLMs), enabling developers to easily integrate AI capabilities into their applications. The library is currently at version 6.7.0 and maintains a regular release cadence with updates and new features.
Common errors
-
ModuleNotFoundError: No module named 'sap_ai_sdk'
cause The `sap-ai-sdk-gen` library is not installed in your Python environment.fixRun `pip install sap-ai-sdk-gen` to install the library. -
sap_ai_sdk.generative_ai.exceptions.ProviderNotFoundException: No provider found for [...]
cause The SDK could not find a configured provider or the specified provider name is incorrect/unconfigured.fixEnsure that the necessary environment variables for your chosen provider (e.g., `OPENAI_API_KEY`, SAP AI Core variables) are correctly set, or explicitly pass the `provider` argument to the `GenerativeAI` client or service methods if multiple are configured. -
pydantic.v1_pydantic_core.core_schema.ValidationError: 1 validation error for CompletionRequest response_format.type value is not a valid enumeration member; permitted: 'text', 'json_object'
cause You are likely using an older version of the SDK or an unsupported configuration for the `response_format` in the `create` call. This error also often appears when API keys are missing, as the request object might not be fully formed or validated against the correct provider schema.fixEnsure you are on the latest SDK version. Check that your API keys are correctly set. For `response_format`, ensure you are passing either `ResponseFormatType.JSON_OBJECT` or `ResponseFormatType.TEXT` (imported from `sap_ai_sdk.generative_ai.types`) as specified by the documentation for the `response_format` parameter, or omit it if not needed. -
sap_ai_sdk.generative_ai.exceptions.ModelNotFoundException: Model 'non-existent-model' not found for provider 'openai'
cause The specified `model_name` does not exist or is not available for the configured provider.fixVerify the `model_name` is correct and supported by your chosen provider (e.g., 'gpt-3.5-turbo', 'gpt-4o' for OpenAI). Refer to the provider's documentation for available models.
Warnings
- breaking The SDK underwent significant refactoring in version 6.x. The `GenerativeAI` class is now the central entry point for all services (completion, chat, image generation, etc.). Direct instantiation of services like `Completion()` with a configuration object is no longer the recommended or supported pattern.
- gotcha Authentication for generative AI providers (e.g., OpenAI, Azure OpenAI, SAP AI Core) relies heavily on environment variables or explicit `Credentials` objects. Misconfigured or missing API keys/credentials will lead to authentication errors (e.g., 401 Unauthorized).
- gotcha When using multiple configured providers or if auto-detection fails, explicitly specifying the `provider` parameter when getting a service (e.g., `ai_client.get_completion_service(provider="openai")`) or in the service call (`completion_service.create(..., provider="openai")`) can prevent `ProviderNotFoundException`.
Install
-
pip install sap-ai-sdk-gen
Imports
- GenerativeAI
from sap_ai_sdk.generative_ai import GenerativeAI
- Completion
from sap_ai_sdk.completion import Completion
from sap_ai_sdk.generative_ai.completion import Completion
- Chat
from sap_ai_sdk.generative_ai.chat import Chat
- ImageGeneration
from sap_ai_sdk.generative_ai.image_generation import ImageGeneration
Quickstart
import os
from sap_ai_sdk.generative_ai import GenerativeAI
from sap_ai_sdk.generative_ai.completion import Completion
# Set your API key as an environment variable (e.g., OPENAI_API_KEY).
# For SAP AI Core, ensure AI_API_URL, AI_CLIENT_ID, AI_CLIENT_SECRET, AI_AUTH_URL are set.
# Example: os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY', '') # For demo, ensure it's loaded.
try:
# Initialize the GenerativeAI client. It automatically detects configured providers.
ai_client = GenerativeAI()
# Get the completion service
completion_service: Completion = ai_client.get_completion_service()
prompt = "Explain the concept of quantum entanglement in simple terms."
model_name = "gpt-3.5-turbo" # Use a model supported by your configured provider
print(f"Requesting completion for model: {model_name}")
response = completion_service.create(
prompt=prompt,
model_name=model_name,
# provider="openai" # Optionally specify if multiple providers are configured
)
print("\nGenerated Text:")
print(response.text)
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure your environment variables (e.g., OPENAI_API_KEY) are correctly set")
print("and that a suitable provider is configured for the chosen model.")