trace#
- class langsmith.run_helpers.trace(name: str, run_type: Literal['tool', 'chain', 'llm', 'retriever', 'embedding', 'prompt', 'parser'] = 'chain', *, inputs: Dict | None = None, extra: Dict | None = None, project_name: str | None = None, parent: RunTree | str | Mapping | None = None, tags: List[str] | None = None, metadata: Mapping[str, Any] | None = None, client: Client | None = None, run_id: UUID | str | None = None, reference_example_id: UUID | str | None = None, exceptions_to_handle: Tuple[Type[BaseException], ...] | None = None, attachments: Dict[str, Tuple[str, bytes] | Attachment] | None = None, **kwargs: Any)[source]#
Manage a LangSmith run in context.
This class can be used as both a synchronous and asynchronous context manager.
- Parameters:
name (str) – Name of the run.
run_type (ls_client.RUN_TYPE_T, optional) – Type of run (e.g., “chain”, “llm”, “tool”). Defaults to “chain”.
inputs (Optional[Dict], optional) – Initial input data for the run. Defaults to None.
project_name (Optional[str], optional) – Project name to associate the run with. Defaults to None.
parent (Optional[Union[run_trees.RunTree, str, Mapping]], optional) – Parent run. Can be a RunTree, dotted order string, or tracing headers. Defaults to None.
tags (Optional[List[str]], optional) – List of tags for the run. Defaults to None.
metadata (Optional[Mapping[str, Any]], optional) – Additional metadata for the run. Defaults to None.
client (Optional[ls_client.Client], optional) – LangSmith client for custom settings. Defaults to None.
run_id (Optional[ls_client.ID_TYPE], optional) – Preset identifier for the run. Defaults to None.
reference_example_id (Optional[ls_client.ID_TYPE], optional) – Associates run with a dataset example. Only for root runs in evaluation. Defaults to None.
exceptions_to_handle (Optional[Tuple[Type[BaseException], ...]], optional) – Exception types to ignore. Defaults to None.
extra (Optional[Dict], optional) – Extra data to send to LangSmith. Use ‘metadata’ instead. Defaults to None.
attachments (Optional[schemas.Attachments]) –
kwargs (Any) –
Examples
Synchronous usage:
>>> 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:
>>> 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:
>>> 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
Initialize the trace context manager.
Warns if unsupported kwargs are passed.
Methods
__init__
(name[, run_type, inputs, extra, ...])Initialize the trace context manager.
- __init__(name: str, run_type: Literal['tool', 'chain', 'llm', 'retriever', 'embedding', 'prompt', 'parser'] = 'chain', *, inputs: Dict | None = None, extra: Dict | None = None, project_name: str | None = None, parent: RunTree | str | Mapping | None = None, tags: List[str] | None = None, metadata: Mapping[str, Any] | None = None, client: Client | None = None, run_id: UUID | str | None = None, reference_example_id: UUID | str | None = None, exceptions_to_handle: Tuple[Type[BaseException], ...] | None = None, attachments: Dict[str, Tuple[str, bytes] | Attachment] | None = None, **kwargs: Any)[source]#
Initialize the trace context manager.
Warns if unsupported kwargs are passed.
- Parameters:
name (str) –
run_type (Literal['tool', 'chain', 'llm', 'retriever', 'embedding', 'prompt', 'parser']) –
inputs (Dict | None) –
extra (Dict | None) –
project_name (str | None) –
parent (RunTree | str | Mapping | None) –
tags (List[str] | None) –
metadata (Mapping[str, Any] | None) –
client (Client | None) –
run_id (UUID | str | None) –
reference_example_id (UUID | str | None) –
exceptions_to_handle (Tuple[Type[BaseException], ...] | None) –
attachments (Dict[str, Tuple[str, bytes] | Attachment] | None) –
kwargs (Any) –