Mistral client
get_mistral_client() returns a standard Mistral SDK client pre-configured with workflow-aware hooks. It handles authentication, telemetry, and observability metadata automatically.
Always use get_mistral_client() inside activities instead of constructing Mistral(api_key=...) directly. A vanilla client does not collect workflow-specific telemetry, does not inject observability metadata, and does not support on-behalf-of credentials.
Quick start
# ❌ Don't — misses telemetry, observability metadata, and proper auth
from mistralai import Mistral
client = Mistral(api_key="your_api_key")# ✅ Do — automatically configured for workflows
from mistralai.workflows.client import get_mistral_client
client = get_mistral_client()What it adds
| Feature | Description |
|---|---|
| Telemetry | Collects Mistral API telemetry for your workflow traces |
| Observability metadata | Adds workflow metadata with execution and activity IDs for workflow tracing |
| On-behalf-of credentials | When use_executor_credentials=True, calls the Mistral API as the triggering user instead of the worker |
| Automatic authentication | Selects the correct credential for your deployment — API key, Service Account token file, or explicit key |
Telemetry
When the workflow's OpenTelemetry provider is enabled, get_mistral_client() ensures that Mistral API calls automatically appear in your workflow traces.
If telemetry is disabled in the worker configuration, the wiring is skipped silently. See Observability for more telemetry details.
Observability metadata
Every outgoing HTTP request gets an x-metadata JSON header containing:
| Field | Description |
|---|---|
execution_id | The workflow execution ID |
run_id | The workflow run ID |
task_id | The activity ID |
attempt | The retry attempt number |
This metadata links each API call to the exact workflow execution and activity attempt.
On-behalf-of credentials
In on-behalf-of workflows, you can call the Mistral API as the triggering user instead of the worker.
Automatic authentication
get_mistral_client() handles authentication automatically. It selects credentials in the following order:
- Explicit
api_keyargument — if you passget_mistral_client(api_key="..."), that key is used directly. - Service Account token file — if
MISTRAL_SA_TOKEN_PATHis set, Service Account authentication is used instead ofMISTRAL_API_KEY. MISTRAL_API_KEYenvironment variable — used as a static API key when neither of the above is set.
In most cases you can call get_mistral_client() with no arguments — the correct token is resolved automatically from your environment.
Using with dependency injection
get_mistral_client works as a Depends() provider. The worker initializes it once at startup and reuses the same client across all activity executions:
import mistralai.workflows as workflows
from mistralai.workflows.client import get_mistral_client
from mistralai.workflows import Depends
@workflows.activity()
async def summarize(text: str, client = Depends(get_mistral_client)) -> str:
response = await client.chat.complete_async(
model="mistral-large-latest",
messages=[{"role": "user", "content": f"Summarize:\n\n{text}"}],
)
return response.choices[0].message.contentFor more on the Depends() pattern and provider lifecycle, see Dependency injection.