Observability
Mistral AI (tracing)
The Mistral AI Python SDK is the official client for Mistral models. respan-instrumentation-mistralai activates the Mistral AI OpenInference wrapper and normalizes emitted spans to the Respan tracing pipeline.
Set up Respan
- Sign up - Create an account at platform.respan.ai
- Create an API key - Generate one on the API keys page
Paste this into your coding agent:
Help me set up Respan tracing with the Mistral AI integration.
Follow https://www.respan.ai/docs/integrations/mistral-tracing.mdSetup
Install packages
pip install respan-ai respan-instrumentation-mistralai mistralai python-dotenvSet environment variables
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
export MISTRAL_API_KEY="YOUR_MISTRAL_API_KEY"Optional:
export RESPAN_BASE_URL="https://api.respan.ai/api"
export MISTRALAI_MODEL="mistral/mistral-small"Initialize and run
import os
from dotenv import load_dotenv
from mistralai.client import Mistral
from respan import Respan, workflow
from respan_instrumentation_mistralai import MistralAIInstrumentor
load_dotenv()
respan = Respan(
api_key=os.environ["RESPAN_API_KEY"],
base_url=os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api"),
instrumentations=[MistralAIInstrumentor()],
)
@workflow(name="mistralai_chat_completion")
def run_chat() -> str:
with Mistral(api_key=os.environ["MISTRAL_API_KEY"]) as client:
response = client.chat.complete(
model=os.getenv("MISTRALAI_MODEL", "mistral/mistral-small"),
messages=[
{
"role": "user",
"content": "Reply with one concise sentence about tracing.",
}
],
)
return str(response.choices[0].message.content or "")
try:
print(run_chat())
finally:
respan.shutdown()View your trace
Open the Traces page and search for the workflow name mistralai_chat_completion.
Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
**instrumentor_kwargs | dict | {} | Extra keyword arguments forwarded to the upstream Mistral AI OpenInference instrumentor. |
Attributes
Attach customer identifiers, thread IDs, workflow names, and metadata to Mistral AI calls with propagate_attributes.
from respan import propagate_attributes
with propagate_attributes(
customer_identifier="user_123",
session_identifier="conversation_456",
trace_group_identifier="mistralai_support_chat.workflow",
metadata={"plan": "pro", "workflow_name": "mistralai_support_chat.workflow"},
):
response = client.chat.complete(
model="mistral/mistral-small",
messages=[{"role": "user", "content": "Summarize our support policy."}],
)| Attribute | Type | Description |
|---|---|---|
customer_identifier | str | Identifies the end user in Respan analytics. |
session_identifier | str | Groups related messages into a conversation. |
trace_group_identifier | str | Groups spans by workflow name. |
metadata | dict | Custom key-value pairs merged with default metadata. |
Examples
Multi-turn chat
response = client.chat.complete(
model="mistral/mistral-small",
messages=[
{"role": "user", "content": "Explain tracing in one sentence."},
{"role": "assistant", "content": "Tracing records how a request moves through a system."},
{"role": "user", "content": "Now make that explanation shorter."},
],
)
print(response.choices[0].message.content)Async chat
import asyncio
import os
from mistralai.client import Mistral
async def main() -> None:
async with Mistral(api_key=os.environ["MISTRAL_API_KEY"]) as client:
response = await client.chat.complete_async(
model="mistral/mistral-small",
messages=[{"role": "user", "content": "Write a short haiku about traces."}],
)
print(response.choices[0].message.content)
asyncio.run(main())