langchainlangsmith

How to specify the LangSmith project name for each Chain?


According to the LangSmith documentation you need to set the LANGCHAIN_PROJECT environment variable to specify the project name in langsmith.

However, if we want to execute multiple chains within a single service but under different project names, how do we specify the project name at runtime per-chain?

We think we have a solution, but it required looking into the LangChain code, so we are concerned this is not the idomatic way to do it and it might cause issues when their API changes.

This is what we think we should do.

  1. Instantiate the chain.
  2. Instantiate a custom langchain tracer but with a specific project name defined at runtime.
  3. Replace the chain's callbacks with the tracer.
        chain = LLMChain(llm=self._chat_model, prompt=prompt, verbose=True)
        tracer = LangChainTracer(project_name="whatever")
        chain.callbacks = CallbackManager(handlers=[tracer])

Thank you in advance for any help.


Solution

  • You can specify project name in the @traceable decorator as an argument.

    @traceable(
        run_type="llm",
        project_name="My Project"
    )
    def my_llm_call(
        ...
    ) -> str:
    

    You can also put it as a part of langsmith_extra when calling the traced function. This takes precedence over the traceable project:

    my_llm_call(
       ...,
       langsmith_extra={"project_name": "My Project 2"},
    )