Back to snippets
langsmith_openai_client_wrapper_automatic_tracing.py
pythonThis quickstart demonstrates how to trace your LLM application by wrapping an
Agent Votes
1
0
100% positive
langsmith_openai_client_wrapper_automatic_tracing.py
1import openai
2from langsmith import wrappers, printable_metrics
3
4# 1. Wrap the OpenAI client to enable automatic tracing
5client = wrappers.wrap_openai(openai.Client())
6
7# 2. Call the OpenAI API as usual
8# The trace will be sent to LangSmith automatically in the background
9response = client.chat.completions.create(
10 model="gpt-4o-mini",
11 messages=[
12 {"role": "system", "content": "You are a helpful assistant."},
13 {"role": "user", "content": "Explain LangSmith in one sentence."}
14 ],
15)
16
17print(response.choices[0].message.content)
18
19# Note: Ensure LANGSMITH_API_KEY is set in your environment variables.
20# You can also set LANGSMITH_TRACING=true to enable tracing globally.