gigachat
Описание
Языки
- Python99,4%
- Makefile0,6%
GigaChat Python SDK
Python SDK for the GigaChat REST API — a large language model.
This library is part of GigaChain and powers langchain-gigachat, the official LangChain integration for GigaChat.
Table of Contents
- Features
- Installation
- Quick Start
- Usage Examples
- Configuration
- Authentication
- SSL Certificates
- Error Handling
- Advanced Features
- API Reference
- Related Projects
- Contributing
- License
Features
- ✅ Chat completions — synchronous and asynchronous
- ✅ Streaming responses — real-time token generation
- ✅ Embeddings — text vectorization
- ✅ Function calling — tool use for building agents
- ✅ Vision — image understanding (multimodal)
- ✅ File operations — upload, retrieve, and delete files
- ✅ Token counting — estimate token usage before requests
- ✅ Multiple auth methods — OAuth credentials, password, TLS certificates, access tokens
- ✅ Automatic retry — configurable exponential backoff for transient errors
- ✅ Fully typed — Pydantic V2 models with
marker for IDE supportpy.typed
Installation
Requirements: Python 3.8 — 3.13
Note: In production, keep TLS verification enabled (default). See SSL Certificates for setup instructions.
Quick Start
Get your GigaChat authorization key
For detailed instructions, see the official documentation.
Configure gigachat package to use TLS certificate
TLS certificate: follow the OS-specific installation steps on Gosuslugi (see SSL Certificates for how to configure / if needed).
Development-only (not recommended):
set or pass to .
Usage Examples
The examples below assume authentication is configured via environment variables (for example,
). See Authentication.GIGACHAT_CREDENTIALS
Basic Chat
Streaming
Receive tokens as they are generated:
Async
Use async/await for non-blocking operations:
Embeddings
Generate vector representations of text:
Note: The
parameter must be passed directly to themodelmethod (default:embeddings()). The"Embeddings"set in themodelconstructor does not affect embeddings.GigaChat()
Function Calling
Enable the model to call functions (tools):
More examples
See the examples/ folder for complete working examples including chat, functions, context variables, AI detection, and vision.
Configuration
Constructor Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| | | Authorization key from GigaChat API |
| | | API scope (see below) |
| | | Default model for requests |
| | | API base URL |
| | | OAuth token endpoint |
| | | Pre-obtained access token (bypasses OAuth) |
| | | Username for password authentication |
| | | Password for password authentication |
| | | Verify SSL certificates |
| | | Path to CA certificate bundle |
| | | Path to client certificate (for mTLS) |
| | | Path to client private key (for mTLS) |
| | | Password for encrypted private key |
| | | Request timeout in seconds |
| | | Maximum concurrent connections |
| | | Maximum retry attempts for transient errors |
| | | Exponential backoff multiplier |
| | | HTTP status codes that trigger retry |
| | | Enable profanity filtering |
| | | Additional API flags |
API Scopes:
| Scope | Description |
|---|---|
| API for individuals (default) |
| API for businesses (prepaid) |
| API for businesses (postpaid) |
Environment Variables
All parameters can be configured via environment variables with the prefix:
Then create a client without any parameters:
Authentication
The library supports four authentication methods:
Authentication priority (when multiple are configured)
If multiple auth inputs are provided at the same time, the SDK applies this priority:
(if set) — overrides any other auth source.custom_headers_cvar["Authorization"](if set) — overrides any other auth source and disables automatic token fetching/refresh (you manage the header value and its lifecycle).authorization_cvar- Explicit
(constructor parameter /access_token) — used as-is. If it fails with 401 and OAuth credentials or user/password are also configured, the SDK will fall back to fetching a new token.GIGACHAT_ACCESS_TOKEN - OAuth
(constructor parameter /credentials) — used to obtain/refresh a token.GIGACHAT_CREDENTIALS - Username/password (
+user) — used to obtain/refresh a token from thepasswordendpoint./token
When both OAuth and username/password are provided, OAuth credentials take precedence for token refresh.
1. Authorization Key (Recommended)
For detailed instructions, see the official documentation.
The authorization key encodes your API scope. If using the B2B or CORP API, specify the scope explicitly:
2. Username and Password
Authenticate with a username and password:
3. TLS Certificates (mTLS)
Authenticate using client certificates for mutual TLS:
4. Access Token
Use a pre-obtained access token (JWT):
Note: Access tokens expire after 30 minutes. Use this method when you manage token lifecycle externally.
Pre-authentication
By default, the library obtains an access token on the first API request. To authenticate immediately:
SSL Certificates
GigaChat endpoints use a certificate chain issued by the Russian Ministry of Digital Development. This section explains how to configure the GigaChat SDK to use the required certificates.
Quick Reference
- What you need: The "Russian Trusted Root CA" certificate file from Gosuslugi
- How to configure: Set
environment variable or passGIGACHAT_CA_BUNDLE_FILEargument toca_bundle_fileGigaChat() - Why: Python HTTP clients typically use their own CA bundle (e.g.,
) instead of the OS trust storecertifi
Configuration
Environment variable (recommended):
Or as argument:
OS-Specific Notes
Download the "Russian Trusted Root CA" certificate from Gosuslugi and configure to point to the downloaded file:
- Windows: Downloaded as
file (e.g.,.cer)Russian Trusted Root CA.cer - macOS/Linux: Downloaded as
file (e.g.,.crt)Russian_Trusted_Root_CA.crt
Example paths:
Development-only: Disable TLS verification (not recommended)
- Environment variable: GIGACHAT_VERIFY_SSL_CERTS=false
- Or pass
toverify_ssl_certs=FalseGigaChat(...)
⚠️ Warning: Disabling certificate verification reduces security and is not recommended for production environments.
Error Handling
The library raises specific exceptions for different error conditions:
Exception Reference
| Exception | HTTP Status | Description |
|---|---|---|
| — | Base exception for all library errors |
| — | Base exception for HTTP response errors |
| 401 | Invalid or expired credentials |
| 400 | Malformed request or invalid parameters |
| 403 | Access denied (insufficient permissions) |
| 404 | Requested resource not found |
| 413 | Request payload too large |
| 429 | Too many requests (use ) |
| 5xx | Server-side error |
Advanced Features
Context Variables
Track requests with custom headers for logging and debugging:
Available context variables:
| Variable | Header | Description |
|---|---|---|
| | Session identifier for grouping requests |
| | Unique request identifier |
| | Client identifier |
| (various) | Dictionary of additional headers |
Header precedence (when multiple sources set the same header):
- Explicit
passed by the SDK (or your code) setsaccess_tokenfirst.Authorization: Bearer ... overrides thatauthorization_cvarheader if it is set.Authorizationis applied last and overrides both (includingcustom_headers_cvar), as well as any other header.Authorization
Retry Configuration
Configure automatic retry with exponential backoff for transient errors:
Avoid “double retry” (important): If you use
through a higher-level library that also retries (for example,gigachat/ LangChain), enable retries in only one layer. Otherwise, the effective number of attempts multiplies (e.g., 3 SDK retries × 3 framework retries).langchain-gigachatRecommended defaults:
- Keep
retries disabled (defaultgigachat) when the outer framework retries.max_retries=0- Or disable retries in the outer framework and configure retries here (recommended if you want one consistent retry policy).
Deprecations
: deprecated by the upstream API. Do not use it in new code.Messages.data_for_context- Use instead: include the relevant information directly in the message
, or attach files viacontent(file IDs) when you need to provide additional context.attachments - Timeline: the SDK will keep accepting
through thedata_for_contextline, but it may be removed in0.x(or earlier if the upstream API removes it).1.0.0
- Use instead: include the relevant information directly in the message
Token Counting
Estimate token usage before sending requests:
Available Models
List available models and their capabilities:
File Operations
Upload and manage files:
Balance Check
Check your remaining token balance (prepaid accounts only):
API Reference
Related Projects
- GigaChain — A set of solutions for developing Russian-language LLM applications and multi-agent systems, with support for LangChain, LangGraph, LangChain4j, as well as GigaChat and other available LLMs. GigaChain covers the full development lifecycle: from prototyping and research to production deployment and ongoing support.
- langchain-gigachat — Official LangChain integration package for GigaChat
Versioning and stability
This project follows SemVer with additional clarity for pre- releases:
- Patch releases (
): Backwards compatible bug fixes and internal changes.0.x.Y - Minor releases (
): May include backwards-incompatible changes. Any breaking changes must be called out in the GitHub Release notes.0.X.0
Stable release gate
To ship a release marked Production/Stable, the following must be true:
- CI is green on
(lint, mypy, unit tests, integration replay).main - Local checks are green (
).make all - Packaging is sane: sdist+wheel build and install from artifacts works (no missing files).
- Integration cassettes are current: re-recorded with real credentials and reviewed for scrubbing.
Contributing
We welcome contributions of all kinds — bug reports, feature requests, documentation improvements, and code contributions!
Quick Start:
For detailed contributing guidelines, please see CONTRIBUTING.md.
This guide covers:
- Development setup and workflow
- Code quality standards and testing
- Commit message guidelines
- Pull request process
- Issue reporting guidelines
- Project architecture
All contributions are licensed under the MIT License.
License
This project is licensed under the MIT License.
Copyright © 2026 GigaChain