Skip to content

Added a X-Trace-Id header

Choose a tag to compare

@ianrothmann ianrothmann released this 16 Jun 15:50
· 5 commits to main since this release

For tracking calls to remote runnables. This needs to be implemented in LangServe as follows:

import contextvars
trace_id_var = contextvars.ContextVar("trace_id", default=None)

async def set_trace_id(x_trace_id: Optional[str] = Header(None)):
    if x_trace_id:
        trace_id_var.set(x_trace_id)

dependencies.append(Depends(set_trace_id))

app = FastAPI(
    title="AINinja Server",
    version="1.0",
    dependencies=dependencies,
)

def config_modifier(config: RunnableConfig, ctx):
    if "tags" not in config:
        config["tags"] = []
    if trace_id_var.get():
        config["tags"].append(trace_id_var.get())
    return config

add_routes(
        app,
        runnable,
        path=path,
        per_req_config_modifier=config_modifier,
    )


This then creates a tag in LangSmith for the trace ID