How to set different LangSmith project names for the same graph in multiple API endpoints

I’m working with a complex workflow that gets used in two different API routes. The same workflow runs at /endpoint-a and also as part of a larger process at /endpoint-b.

My issue is with logging to LangSmith. I need the traces to go to different projects depending on which endpoint triggered the workflow:

  • When called from /endpoint-a, logs should go to “ProjectAlpha”
  • When called from /endpoint-b, logs should go to “ProjectBeta”

I can’t use the @traceable decorator since the same functions are shared between both use cases.

Right now I’m setting the project like this:

os.environ["LANGCHAIN_PROJECT"] = settings.project_name

But this only works for one scenario. I’ve been looking into RunTrees but I’m stuck on how to properly configure it:

workflow_config = {
    "execution_id": request.session_id,
    "max_iterations": 100,
    "configurable": {
        "session_id": str(uuid.uuid4()),
        "checkpoint_id": ""
    },
}

my_workflow = workflow_builder.compile()

trace_runner = RunTree(
    run_type="workflow",
    name="My Workflow",
    inputs=initial_state,
    project_name="ProjectBeta"
)

# How do I execute my_workflow with workflow_config and capture the output?

trace_runner.end(outputs=???)
trace_runner.post()

Is there a way to dynamically specify which LangSmith project to use for each execution?

Just pass the project name directly in your config when you run the workflow. Add “project_name”: “ProjectAlpha” or “ProjectBeta” to your workflow_config under configurable, then call my_workflow.invoke(initial_state, config=workflow_config). This’ll override the env variable for each run without needing runtrees.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.