Skip to main content
The AsyncClient class provides an async interface for interacting with the LangSmith API. It uses httpx for async HTTP requests and is ideal for async applications.

Constructor

from langsmith import AsyncClient

client = AsyncClient(
    api_url="https://api.smith.langchain.com",
    api_key="your-api-key"
)
api_url
str | None
URL for the LangSmith API. Defaults to the LANGCHAIN_ENDPOINT or LANGSMITH_ENDPOINT environment variable.
api_key
str | None
API key for authentication. Defaults to the LANGCHAIN_API_KEY or LANGSMITH_API_KEY environment variable.
timeout_ms
int | tuple[int, int, int, int] | None
Timeout for requests in milliseconds. Can be a single value or a tuple of timeouts.
retry_config
Mapping[str, Any] | None
Configuration for request retries. Default is {"max_retries": 3}.
web_url
str | None
URL for the LangSmith web application.
disable_prompt_cache
bool
Whether to disable prompt caching for this client. Default is False.

Usage with context manager

The async client should be used as an async context manager:
async with AsyncClient() as client:
    await client.acreate_run(
        name="my-chain",
        inputs={"query": "What is LangSmith?"},
        run_type="chain"
    )

Core methods

acreate_run

Asynchronously create a new run in LangSmith.
await client.acreate_run(
    name="my-chain",
    inputs={"query": "What is LangSmith?"},
    run_type="chain",
    project_name="my-project"
)
name
str
required
Name of the run.
inputs
dict[str, Any]
required
Input data for the run.
run_type
str
required
Type of run: "llm", "chain", "tool", "retriever", or "prompt".
project_name
str | None
Project to log the run to.
run_id
UUID | str | None
Unique identifier for the run.
parent_run_id
UUID | str | None
ID of the parent run for nested traces.
tags
list[str] | None
Tags for filtering and organization.
metadata
dict[str, Any] | None
Additional metadata.

aupdate_run

Asynchronously update an existing run.
await client.aupdate_run(
    run_id=run_id,
    outputs={"answer": "LangSmith is a platform for..."},
    end_time=datetime.now()
)
run_id
UUID | str
required
ID of the run to update.
outputs
dict[str, Any] | None
Output data from the run.
error
str | None
Error message if the run failed.
end_time
datetime | None
End time of the run.

acreate_feedback

Asynchronously create feedback for a run.
await client.acreate_feedback(
    run_id=run_id,
    key="accuracy",
    score=0.95
)
run_id
UUID | str
required
ID of the run to attach feedback to.
key
str
required
Name of the metric.
score
float | int | bool | None
Numeric score.
comment
str | None
Explanation for the feedback.

Dataset methods

acreate_dataset

Asynchronously create a new dataset.
dataset = await client.acreate_dataset(
    dataset_name="my-dataset",
    description="QA pairs for evaluation"
)
dataset_name
str
required
Name of the dataset.
description
str | None
Description of the dataset.
return
Dataset
The created dataset object.

acreate_example

Asynchronously add an example to a dataset.
example = await client.acreate_example(
    inputs={"question": "What is LangSmith?"},
    outputs={"answer": "A platform for LLM development"},
    dataset_id=dataset.id
)
inputs
dict[str, Any]
required
Input data for the example.
outputs
dict[str, Any] | None
Expected output data.
dataset_id
UUID | str
required
ID of the dataset.
return
Example
The created example object.

alist_datasets

Asynchronously list all datasets.
async for dataset in client.alist_datasets():
    print(dataset.name)
return
AsyncIterator[Dataset]
Async iterator of dataset objects.

Prompt methods

apull_prompt

Asynchronously pull a prompt from the hub.
prompt = await client.apull_prompt("my-prompt")
prompt_identifier
str
required
Prompt identifier in format "name", "owner/name", or with commit hash.
include_model
bool | None
Whether to include model configuration.
return
Any
The prompt object.

apush_prompt

Asynchronously push a prompt to the hub.
await client.apush_prompt(
    "my-prompt",
    object=prompt_template
)
prompt_identifier
str
required
Name for the prompt.
object
Any
required
The prompt object to push.
return
str
URL of the pushed prompt.

Cleanup

Always close the async client when done:
# Using context manager (recommended)
async with AsyncClient() as client:
    # Use client
    pass

# Or manually
client = AsyncClient()
try:
    # Use client
    pass
finally:
    await client.aclose()

Build docs developers (and LLMs) love