Anthropic API client library for kiarina namespace
Project description
kiarina-lib-anthropic
A Python library for Anthropic API integration with configuration management using pydantic-settings-manager.
Features
- Configuration Management: Use
pydantic-settings-managerfor flexible configuration - Type Safety: Full type hints and Pydantic validation
- Secure Credential Handling: API keys are protected using
SecretStr - Multiple Configurations: Support for multiple named configurations (e.g., different projects/environments)
- Environment Variable Support: Configure via environment variables with
KIARINA_LIB_ANTHROPIC_prefix - Custom Base URL: Support for custom Anthropic-compatible API endpoints
Installation
pip install kiarina-lib-anthropic
Quick Start
Basic Usage
from kiarina.lib.anthropic import AnthropicSettings, settings_manager
# Configure Anthropic API
settings_manager.user_config = {
"default": {
"api_key": "sk-ant-your-api-key-here"
}
}
# Get settings
settings = settings_manager.settings
print(f"API Key configured: {settings.api_key.get_secret_value()[:10]}...")
Environment Variable Configuration
Configure authentication using environment variables:
export KIARINA_LIB_ANTHROPIC_API_KEY="sk-ant-your-api-key-here"
from kiarina.lib.anthropic import settings_manager
# Settings are automatically loaded from environment variables
settings = settings_manager.settings
print(f"API Key configured: {settings.api_key.get_secret_value()[:10]}...")
Multiple Configurations
Manage multiple Anthropic configurations (e.g., different projects or environments):
from kiarina.lib.anthropic import settings_manager
# Configure multiple projects
settings_manager.user_config = {
"project_a": {
"api_key": "sk-ant-project-a-key"
},
"project_b": {
"api_key": "sk-ant-project-b-key"
}
}
# Switch between configurations
settings_manager.active_key = "project_a"
project_a_settings = settings_manager.settings
print(f"Project A API Key: {project_a_settings.api_key.get_secret_value()[:10]}...")
settings_manager.active_key = "project_b"
project_b_settings = settings_manager.settings
print(f"Project B API Key: {project_b_settings.api_key.get_secret_value()[:10]}...")
Custom Base URL
Use with Anthropic-compatible APIs:
from kiarina.lib.anthropic import settings_manager
settings_manager.user_config = {
"custom": {
"api_key": "your-custom-key",
"base_url": "https://custom.anthropic-compatible.api.com"
}
}
settings = settings_manager.settings
print(f"Base URL: {settings.base_url}")
Configuration
This library uses pydantic-settings-manager for flexible configuration management.
AnthropicSettings
The AnthropicSettings class provides the following configuration fields:
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
api_key |
SecretStr |
Yes | - | Anthropic API key (masked in logs) |
base_url |
str | None |
No | None |
Custom base URL for Anthropic-compatible APIs |
Environment Variables
All settings can be configured via environment variables with the KIARINA_LIB_ANTHROPIC_ prefix:
# API Key (required)
export KIARINA_LIB_ANTHROPIC_API_KEY="sk-ant-your-api-key"
# Custom Base URL (optional)
export KIARINA_LIB_ANTHROPIC_BASE_URL="https://api.anthropic.com"
Programmatic Configuration
from pydantic import SecretStr
from kiarina.lib.anthropic import AnthropicSettings, settings_manager
# Direct settings object
settings = AnthropicSettings(
api_key=SecretStr("sk-ant-your-api-key")
)
# Via settings manager
settings_manager.user_config = {
"default": {
"api_key": "sk-ant-your-api-key" # Automatically converted to SecretStr
}
}
Runtime Overrides
from kiarina.lib.anthropic import settings_manager
# Override specific settings at runtime
settings_manager.cli_args = {
"base_url": "https://custom.api.com"
}
settings = settings_manager.settings
print(f"Base URL: {settings.base_url}") # Uses overridden value
Security
API Key Protection
API keys are stored using Pydantic's SecretStr type, which provides the following security benefits:
- Masked in logs: Keys are displayed as
**********in string representations - Prevents accidental exposure: Keys won't appear in debug output or error messages
- Explicit access required: Must use
.get_secret_value()to access the actual key
from kiarina.lib.anthropic import settings_manager
settings = settings_manager.settings
# API key is masked in string representation
print(settings) # api_key=SecretStr('**********')
# Explicit access to get the actual key
api_key = settings.api_key.get_secret_value()
API Reference
AnthropicSettings
class AnthropicSettings(BaseSettings):
api_key: SecretStr
base_url: str | None = None
Pydantic settings model for Anthropic API configuration.
Fields:
api_key(SecretStr): Anthropic API key (protected)base_url(str | None): Optional custom base URL for Anthropic-compatible APIs
settings_manager
settings_manager: SettingsManager[AnthropicSettings]
Global settings manager instance for Anthropic configuration. See: pydantic-settings-manager
Development
Prerequisites
- Python 3.12+
Setup
# Clone the repository
git clone https://github.com/kiarina/kiarina-python.git
cd kiarina-python
# Setup development environment
mise run setup
Running Tests
# Run format, lint, type checks and tests
mise run package kiarina-lib-anthropic
# Coverage report
mise run package:test kiarina-lib-anthropic --coverage
Dependencies
- pydantic-settings - Settings management
- pydantic-settings-manager - Advanced settings management
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
This is a personal project, but contributions are welcome! Please feel free to submit issues or pull requests.
Related Projects
- kiarina-python - The main monorepo containing this package
- pydantic-settings-manager - Configuration management library used by this package
- kiarina-lib-openai - Similar library for OpenAI API
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kiarina_lib_anthropic-1.20.1.tar.gz.
File metadata
- Download URL: kiarina_lib_anthropic-1.20.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a959d476f4ac610d332d2b2f9b9ff5a94a9c44fbc24d6cb3f17f532b94cf568
|
|
| MD5 |
0ea919f11f697dbfbe98c2947d93a55f
|
|
| BLAKE2b-256 |
1f45a46717a07f1ba63068ba98a65fa88237495456f6d9562521586ac4102544
|
File details
Details for the file kiarina_lib_anthropic-1.20.1-py3-none-any.whl.
File metadata
- Download URL: kiarina_lib_anthropic-1.20.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11acdaad331062e82286bcd0360034cfb58ecc8e6caf4a5cbfbc89bf3c698f16
|
|
| MD5 |
decd3f56332d84980d0659872d105fde
|
|
| BLAKE2b-256 |
3a8ccb072691e19afd179121e03ad3f07f012366c5a4764ca4d071394ae6f25a
|