Query traces
Before diving into this content, it might be helpful to read the following:
If you are looking to export a large volume of traces, we recommen that your use the Bulk Data Export functionality, as it will better handle large data volumes and will support automatic retries, and parallelization across partitions.
The recommended way to query runs (the span data in LangSmith traces) is to use the list_runs
method in the SDK or /runs/query
endpoint in the API.
LangSmith stores traces in a simple format that is specified in the Run (span) data format.
Use filter arguments
For simple queries, you don't have to rely on our query syntax. You can use the filter arguments specified in the filter arguments reference.
Initialize the client before running the below code snippets.
- Python
- TypeScript
from langsmith import Client
client = Client()
import { Client, Run } from "langsmith";
const client = new Client();
Below are some examples of ways to list runs using keyword arguments:
List all runs in a project
- Python
- TypeScript
project_runs = client.list_runs(project_name="<your_project>")
// Download runs in a project
const projectRuns: Run[] = [];
for await (const run of client.listRuns({
projectName: "<your_project>",
})) {
projectRuns.push(run);
};
List LLM and Chat runs in the last 24 hours
- Python
- TypeScript
todays_llm_runs = client.list_runs(
project_name="<your_project>",
start_time=datetime.now() - timedelta(days=1),
run_type="llm",
)
const todaysLlmRuns: Run[] = [];
for await (const run of client.listRuns({
projectName: "<your_project>",
startTime: new Date(Date.now() - 1000 * 60 * 60 * 24),
runType: "llm",
})) {
todaysLlmRuns.push(run);
};
List root runs in a project
Root runs are runs that have no parents. These are assigned a value of True
for is_root
. You can use this to filter for root runs.
- Python
- TypeScript
root_runs = client.list_runs(
project_name="<your_project>",
is_root=True
)
const rootRuns: Run[] = [];
for await (const run of client.listRuns({
projectName: "<your_project>",
isRoot: 1,
})) {
rootRuns.push(run);
};
List runs without errors
- Python
- TypeScript
correct_runs = client.list_runs(project_name="<your_project>", error=False)
const correctRuns: Run[] = [];
for await (const run of client.listRuns({
projectName: "<your_project>",
error: false,
})) {
correctRuns.push(run);
};
List runs by run ID
If you provide a list of run IDs in the way described above, it will ignore all other filtering arguments like project_name
, run_type
, etc. and directly return the runs matching the given IDs.
If you have a list of run IDs, you can list them directly:
- Python
- TypeScript
run_ids = ['a36092d2-4ad5-4fb4-9c0d-0dba9a2ed836','9398e6be-964f-4aa4-8ae9-ad78cd4b7074']
selected_runs = client.list_runs(id=run_ids)
const runIds = [
"a36092d2-4ad5-4fb4-9c0d-0dba9a2ed836",
"9398e6be-964f-4aa4-8ae9-ad78cd4b7074",
];
const selectedRuns: Run[] = [];
for await (const run of client.listRuns({
id: runIds,
})) {
selectedRuns.push(run);
};
Use filter query language
For more complex queries, you can use the query language described in the filter query language reference.
List all root runs in a conversational thread
This is the way to fetch runs in a conversational thread. For more information on setting up threads, refer to our how-to guide on setting up threads.
Threads are grouped by setting a shared thread ID. The LangSmith UI lets you use any one of the following three metadata keys: session_id
, conversation_id
, or thread_id
. The following query matches on any of them.
- Python
- TypeScript
group_key = "<your_thread_id>"
filter_string = f'and(in(metadata_key, ["session_id","conversation_id","thread_id"]), eq(metadata_value, "{group_key}"))'
thread_runs = client.list_runs(
project_name="<your_project>",
filter=filter_string,
is_root=True
)
const groupKey = "<your_thread_id>";
const filterString = `and(in(metadata_key, ["session_id","conversation_id","thread_id"]), eq(metadata_value, "${groupKey}"))`;
const threadRuns: Run[] = [];
for await (const run of client.listRuns({
projectName: "<your_project>",
filter: filterString,
isRoot: true
})) {
threadRuns.push(run);
};
List all runs called "extractor" whose root of the trace was assigned feedback "user_score" score of 1
- Python
- TypeScript
client.list_runs(
project_name="<your_project>",
filter='eq(name, "extractor")',
trace_filter='and(eq(feedback_key, "user_score"), eq(feedback_score, 1))'
)
client.listRuns({
projectName: "<your_project>",
filter: 'eq(name, "extractor")',
traceFilter: 'and(eq(feedback_key, "user_score"), eq(feedback_score, 1))'
})
List runs with "star_rating" key whose score is greater than 4
- Python
- TypeScript
client.list_runs(
project_name="<your_project>",
filter='and(eq(feedback_key, "star_rating"), gt(feedback_score, 4))'
)
client.listRuns({
projectName: "<your_project>",
filter: 'and(eq(feedback_key, "star_rating"), gt(feedback_score, 4))'
})
List runs that took longer than 5 seconds to complete
- Python
- TypeScript
client.list_runs(project_name="<your_project>", filter='gt(latency, "5s")')
client.listRuns({projectName: "<your_project>", filter: 'gt(latency, "5s")'})
List all runs where total_tokens
is greater than 5000
- Python
- TypeScript
client.list_runs(project_name="<your_project>", filter='gt(total_tokens, 5000)')
client.listRuns({projectName: "<your_project>", filter: 'gt(total_tokens, 5000)'})
List all runs that have "error" not equal to null
- Python
- TypeScript
client.list_runs(project_name="<your_project>", filter='neq(error, null)')
client.listRuns({projectName: "<your_project>", filter: 'neq(error, null)'})
List all runs where start_time
is greater than a specific timestamp
- Python
- TypeScript
client.list_runs(project_name="<your_project>", filter='gt(start_time, "2023-07-15T12:34:56Z")')
client.listRuns({projectName: "<your_project>", filter: 'gt(start_time, "2023-07-15T12:34:56Z")'})
List all runs that contain the string "substring"
- Python
- TypeScript
client.list_runs(project_name="<your_project>", filter='search("substring")')
client.listRuns({projectName: "<your_project>", filter: 'search("substring")'})
List all runs that are tagged with the git hash "2aa1cf4"
- Python
- TypeScript
client.list_runs(project_name="<your_project>", filter='has(tags, "2aa1cf4")')
client.listRuns({projectName: "<your_project>", filter: 'has(tags, "2aa1cf4")'})
List all "chain" type runs that took more than 10 seconds and
had total_tokens
greater than 5000
- Python
- TypeScript
client.list_runs(
project_name="<your_project>",
filter='and(eq(run_type, "chain"), gt(latency, 10), gt(total_tokens, 5000))'
)
client.listRuns({
projectName: "<your_project>",
filter: 'and(eq(run_type, "chain"), gt(latency, 10), gt(total_tokens, 5000))'
})
List all runs that started after a specific timestamp and either
have "error" not equal to null or a "Correctness" feedback score equal to 0
- Python
- TypeScript
client.list_runs(
project_name="<your_project>",
filter='and(gt(start_time, "2023-07-15T12:34:56Z"), or(neq(error, null), and(eq(feedback_key, "Correctness"), eq(feedback_score, 0.0))))'
)
client.listRuns({
projectName: "<your_project>",
filter: 'and(gt(start_time, "2023-07-15T12:34:56Z"), or(neq(error, null), and(eq(feedback_key, "Correctness"), eq(feedback_score, 0.0))))'
})
Complex query: List all runs where tags
include "experimental" or "beta" and
latency
is greater than 2 seconds
- Python
- TypeScript
client.list_runs(
project_name="<your_project>",
filter='and(or(has(tags, "experimental"), has(tags, "beta")), gt(latency, 2))'
)
client.listRuns({
projectName: "<your_project>",
filter: 'and(or(has(tags, "experimental"), has(tags, "beta")), gt(latency, 2))'
})
Search trace trees by full text You can use the search()
function without
any specific field to do a full text search across all string fields in a run. This allows you to quickly find traces that match a search term.
- Python
- TypeScript
client.list_runs(
project_name="<your_project>",
filter='search("image classification")'
)
client.listRuns({
projectName: "<your_project>",
filter: 'search("image classification")'
})
Check for presence of metadata
If you want to check for the presence of metadata, you can use the eq
operator, optionally with an and
statement to match by value. This is useful if you want to log more structured information
about your runs.
- Python
- TypeScript
to_search = {
"user_id": ""
}
# Check for any run with the "user_id" metadata key
client.list_runs(
project_name="default",
filter="eq(metadata_key, 'user_id')"
)
# Check for runs with user_id=4070f233-f61e-44eb-bff1-da3c163895a3
client.list_runs(
project_name="default",
filter="and(eq(metadata_key, 'user_id'), eq(metadata_value, '4070f233-f61e-44eb-bff1-da3c163895a3'))"
)
// Check for any run with the "user_id" metadata key
client.listRuns({
projectName: 'default',
filter: `eq(metadata_key, 'user_id')`
});
// Check for runs with user_id=4070f233-f61e-44eb-bff1-da3c163895a3
client.listRuns({
projectName: 'default',
filter: `and(eq(metadata_key, 'user_id'), eq(metadata_value, '4070f233-f61e-44eb-bff1-da3c163895a3'))`
});