Run Helpers¶
langsmith.run_helpers
¶
Decorator for creating a run tree from functions.
FUNCTION | DESCRIPTION |
---|---|
get_current_run_tree |
Get the current run tree. |
get_tracing_context |
Get the current tracing context. |
tracing_context |
Set the tracing context for a block of code. |
is_traceable_function |
Check if a function is @traceable decorated. |
ensure_traceable |
Ensure that a function is traceable. |
is_async |
Inspect function or wrapped function to see if it is async. |
traceable |
Trace a function with langsmith. |
as_runnable |
Convert a function wrapped by the LangSmith @traceable decorator to a Runnable. |
LangSmithExtra
¶
SupportsLangsmithExtra
¶
Bases: Protocol
, Generic[P, R]
Implementations of this Protocol accept an optional langsmith_extra parameter.
PARAMETER | DESCRIPTION |
---|---|
*args
|
Variable length arguments.
|
langsmith_extra
|
Optional dictionary of additional parameters for Langsmith.
TYPE:
|
**kwargs
|
Keyword arguments.
|
RETURNS | DESCRIPTION |
---|---|
R
|
The return type of the callable. |
METHOD | DESCRIPTION |
---|---|
__call__ |
Call the instance when it is called as a function. |
__call__
¶
__call__(
*args: args, langsmith_extra: Optional[LangSmithExtra] = None, **kwargs: kwargs
) -> R
Call the instance when it is called as a function.
PARAMETER | DESCRIPTION |
---|---|
*args
|
Variable length argument list.
TYPE:
|
langsmith_extra
|
Optional dictionary containing additional parameters specific to Langsmith.
TYPE:
|
**kwargs
|
Arbitrary keyword arguments.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
R
|
The return value of the method.
TYPE:
|
trace
¶
Manage a LangSmith run in context.
This class can be used as both a synchronous and asynchronous context manager.
PARAMETER | DESCRIPTION |
---|---|
name
|
Name of the run.
TYPE:
|
run_type
|
Type of run (e.g., "chain", "llm", "tool"). Defaults to "chain".
TYPE:
|
inputs
|
Initial input data for the run. Defaults to None.
TYPE:
|
project_name
|
Project name to associate the run with. Defaults to None. |
parent
|
Parent run. Can be a RunTree, dotted order string, or tracing headers. Defaults to None. |
tags
|
List of tags for the run. Defaults to None. |
metadata
|
Additional metadata for the run. Defaults to None. |
client
|
LangSmith client for custom settings. Defaults to None. |
run_id
|
Preset identifier for the run. Defaults to None.
TYPE:
|
reference_example_id
|
Associates run with a dataset example. Only for root runs in evaluation. Defaults to None.
TYPE:
|
exceptions_to_handle
|
Exception types to ignore. Defaults to None.
TYPE:
|
extra
|
Extra data to send to LangSmith. Use 'metadata' instead. Defaults to None.
TYPE:
|
Examples:
Synchronous usage:
.. code-block:: python
>>> with trace("My Operation", run_type="tool", tags=["important"]) as run:
... result = "foo" # Perform operation
... run.metadata["some-key"] = "some-value"
... run.end(outputs={"result": result})
Asynchronous usage:
.. code-block:: python
>>> async def main():
... async with trace("Async Operation", run_type="tool", tags=["async"]) as run:
... result = "foo" # Await async operation
... run.metadata["some-key"] = "some-value"
... # "end" just adds the outputs and sets error to None
... # The actual patching of the run happens when the context exits
... run.end(outputs={"result": result})
>>> asyncio.run(main())
Handling specific exceptions:
.. code-block:: python
>>> import pytest
>>> import sys
>>> with trace("Test", exceptions_to_handle=(pytest.skip.Exception,)):
... if sys.platform == "win32": # Just an example
... pytest.skip("Skipping test for windows")
... result = "foo" # Perform test operation
METHOD | DESCRIPTION |
---|---|
__init__ |
Initialize the trace context manager. |
__enter__ |
Enter the context manager synchronously. |
__exit__ |
Exit the context manager synchronously. |
__aenter__ |
Enter the context manager asynchronously. |
__aexit__ |
Exit the context manager asynchronously. |
__init__
¶
__init__(
name: str,
run_type: RUN_TYPE_T = "chain",
*,
inputs: Optional[dict] = None,
extra: Optional[dict] = None,
project_name: Optional[str] = None,
parent: Optional[Union[RunTree, str, Mapping, Literal["ignore"]]] = None,
tags: Optional[list[str]] = None,
metadata: Optional[Mapping[str, Any]] = None,
client: Optional[Client] = None,
run_id: Optional[ID_TYPE] = None,
reference_example_id: Optional[ID_TYPE] = None,
exceptions_to_handle: Optional[tuple[type[BaseException], ...]] = None,
attachments: Optional[Attachments] = None,
**kwargs: Any,
)
Initialize the trace context manager.
Warns if unsupported kwargs are passed.
__enter__
¶
__enter__() -> RunTree
Enter the context manager synchronously.
RETURNS | DESCRIPTION |
---|---|
RunTree
|
run_trees.RunTree: The newly created run. |
__exit__
¶
__exit__(
exc_type: Optional[type[BaseException]] = None,
exc_value: Optional[BaseException] = None,
traceback: Optional[TracebackType] = None,
) -> None
Exit the context manager synchronously.
PARAMETER | DESCRIPTION |
---|---|
exc_type
|
The type of the exception that occurred, if any.
TYPE:
|
exc_value
|
The exception instance that occurred, if any.
TYPE:
|
traceback
|
The traceback object associated with the exception, if any.
TYPE:
|
__aenter__
async
¶
__aenter__() -> RunTree
Enter the context manager asynchronously.
RETURNS | DESCRIPTION |
---|---|
RunTree
|
run_trees.RunTree: The newly created run. |
__aexit__
async
¶
__aexit__(
exc_type: Optional[type[BaseException]] = None,
exc_value: Optional[BaseException] = None,
traceback: Optional[TracebackType] = None,
) -> None
Exit the context manager asynchronously.
PARAMETER | DESCRIPTION |
---|---|
exc_type
|
The type of the exception that occurred, if any.
TYPE:
|
exc_value
|
The exception instance that occurred, if any.
TYPE:
|
traceback
|
The traceback object associated with the exception, if any.
TYPE:
|
get_tracing_context
¶
Get the current tracing context.
tracing_context
¶
tracing_context(
*,
project_name: Optional[str] = None,
tags: Optional[list[str]] = None,
metadata: Optional[dict[str, Any]] = None,
parent: Optional[Union[RunTree, Mapping, str, Literal[False]]] = None,
enabled: Optional[Union[bool, Literal["local"]]] = None,
client: Optional[Client] = None,
replicas: Optional[Sequence[WriteReplica]] = None,
distributed_parent_id: Optional[str] = None,
**kwargs: Any,
) -> Generator[None, None, None]
Set the tracing context for a block of code.
PARAMETER | DESCRIPTION |
---|---|
project_name
|
The name of the project to log the run to. Defaults to None. |
tags
|
The tags to add to the run. Defaults to None. |
metadata
|
The metadata to add to the run. Defaults to None. |
parent
|
The parent run to use for the context. Can be a Run/RunTree object, request headers (for distributed tracing), or the dotted order string. Defaults to None.
TYPE:
|
client
|
The client to use for logging the run to LangSmith. Defaults to None, |
enabled
|
Whether tracing is enabled. Defaults to None, meaning it will use the current context value or environment variables. |
replicas
|
A sequence of WriteReplica dictionaries to send runs to. Example: [{"api_url": "https://api.example.com", "api_key": "key", "project_name": "proj"}] or [{"project_name": "my_experiment", "updates": {"reference_example_id": None}}] |
distributed_parent_id
|
The distributed parent ID for distributed tracing. Defaults to None. |
is_traceable_function
¶
is_traceable_function(func: Any) -> TypeGuard[SupportsLangsmithExtra[P, R]]
Check if a function is @traceable decorated.
ensure_traceable
¶
ensure_traceable(
func: Callable[P, R],
*,
name: Optional[str] = None,
metadata: Optional[Mapping[str, Any]] = None,
tags: Optional[list[str]] = None,
client: Optional[Client] = None,
reduce_fn: Optional[Callable[[Sequence], Union[dict, str]]] = None,
project_name: Optional[str] = None,
process_inputs: Optional[Callable[[dict], dict]] = None,
process_outputs: Optional[Callable[..., dict]] = None,
process_chunk: Optional[Callable] = None,
) -> SupportsLangsmithExtra[P, R]
Ensure that a function is traceable.
is_async
¶
Inspect function or wrapped function to see if it is async.
traceable
¶
Trace a function with langsmith.
PARAMETER | DESCRIPTION |
---|---|
run_type
|
The type of run (span) to create. Examples: llm, chain, tool, prompt, retriever, etc. Defaults to "chain".
|
name
|
The name of the run. Defaults to the function name.
|
metadata
|
The metadata to add to the run. Defaults to None.
|
tags
|
The tags to add to the run. Defaults to None.
|
client
|
The client to use for logging the run to LangSmith. Defaults to None, which will use the default client.
|
reduce_fn
|
A function to reduce the output of the function if the function returns a generator. Defaults to None, which means the values will be logged as a list. Note: if the iterator is never exhausted (e.g. the function returns an infinite generator), this will never be called, and the run itself will be stuck in a pending state.
|
project_name
|
The name of the project to log the run to. Defaults to None, which will use the default project.
|
process_inputs
|
Custom serialization / processing function for inputs. Defaults to None.
|
process_outputs
|
Custom serialization / processing function for outputs. Defaults to None.
|
dangerously_allow_filesystem
|
Whether to allow filesystem access for attachments. Defaults to False. Traces that reference local filepaths will be uploaded to LangSmith. In general, network-hosted applications should not be using this because referenced files are usually on the user's machine, not the host machine.
|
RETURNS | DESCRIPTION |
---|---|
Union[Callable, Callable[[Callable], Callable]]
|
Union[Callable, Callable[[Callable], Callable]]: The decorated function. |
Note
- Requires that LANGSMITH_TRACING_V2 be set to 'true' in the environment.
Examples:
Basic usage:
.. code-block:: python
@traceable
def my_function(x: float, y: float) -> float:
return x + y
my_function(5, 6)
@traceable
async def my_async_function(query_params: dict) -> dict:
async with httpx.AsyncClient() as http_client:
response = await http_client.get(
"https://api.example.com/data",
params=query_params,
)
return response.json()
asyncio.run(my_async_function({"param": "value"}))
Streaming data with a generator:
.. code-block:: python
@traceable
def my_generator(n: int) -> Iterable:
for i in range(n):
yield i
for item in my_generator(5):
print(item)
Async streaming data:
.. code-block:: python
@traceable
async def my_async_generator(query_params: dict) -> Iterable:
async with httpx.AsyncClient() as http_client:
response = await http_client.get(
"https://api.example.com/data",
params=query_params,
)
for item in response.json():
yield item
async def async_code():
async for item in my_async_generator({"param": "value"}):
print(item)
asyncio.run(async_code())
Specifying a run type and name:
.. code-block:: python
@traceable(name="CustomName", run_type="tool")
def another_function(a: float, b: float) -> float:
return a * b
another_function(5, 6)
Logging with custom metadata and tags:
.. code-block:: python
@traceable(
metadata={"version": "1.0", "author": "John Doe"}, tags=["beta", "test"]
)
def tagged_function(x):
return x**2
tagged_function(5)
Specifying a custom client and project name:
.. code-block:: python
custom_client = Client(api_key="your_api_key")
@traceable(client=custom_client, project_name="My Special Project")
def project_specific_function(data):
return data
project_specific_function({"data": "to process"})
Manually passing langsmith_extra:
.. code-block:: python
@traceable
def manual_extra_function(x):
return x**2
manual_extra_function(5, langsmith_extra={"metadata": {"version": "1.0"}})
as_runnable
¶
Convert a function wrapped by the LangSmith @traceable decorator to a Runnable.
PARAMETER | DESCRIPTION |
---|---|
traceable_fn
|
The function wrapped by the @traceable decorator.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Runnable
|
A Runnable object that maintains a consistent LangSmith tracing context.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ImportError
|
If langchain module is not installed. |
ValueError
|
If the provided function is not wrapped by the @traceable decorator. |
Example
@traceable ... def my_function(input_data): ... # Function implementation ... pass runnable = as_runnable(my_function)