diff --git a/docs/tracing/faq/logging_and_viewing.mdx b/docs/tracing/faq/logging_and_viewing.mdx index 6a338b1b..6eca77d5 100644 --- a/docs/tracing/faq/logging_and_viewing.mdx +++ b/docs/tracing/faq/logging_and_viewing.mdx @@ -286,6 +286,7 @@ Then the server (or other service) can continue the trace by passing the headers ```python # server.py from langsmith import traceable +from langsmith.run_helpers import tracing_context from fastapi import FastAPI, Request @@ -302,10 +303,29 @@ async def fake_route(request: Request): # request.headers: {"langsmith-trace": "..."} # as well as optional metadata/tags in `baggage` # highlight-next-line - return await my_application(langsmith_extra={"parent": request.headers}) + with tracing_context(parent=request.headers): + return await my_application() ``` +The example above uses the `tracing_context` context manager. You can also directly specify the parent run context in the `trace` context manager or `traceable` decorator. + +```python +from langsmith.run_helpers import traceable, trace +# ... same as above +@app.post("/my-route") +async def fake_route(request: Request): + # request.headers: {"langsmith-trace": "..."} + # as well as optional metadata/tags in `baggage` + # highlight-next-line + my_application(langsmith_extra={"parent": request.headers}) + # Or using the `trace` context manager + with trace(parent=request.headers) as run_tree: + ... + run_tree.end(outputs={"answer": "42"}) + ... +``` + ### Turning off tracing If you've decided you no longer want to trace your runs, you can remove the environment variables configured to start tracing in the first place.