Langsmith not displaying logs when running LangChain code in Google Colab

I’m new to LangChain and Langsmith, and I’m having difficulty understanding why my logs aren’t appearing in Langsmith while executing this code in Google Colab. The code functions properly, but nothing shows up in the Langsmith dashboard.

from langchain_core.documents import Document
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.prompts import ChatPromptTemplate
from langchain_community.vectorstores import FAISS
from langchain_openai import AzureOpenAIEmbeddings
import logging
from langchain.chains import create_retrieval_chain
from langsmith import Client
from langchain_core.messages import HumanMessage, AIMessage
from langchain_core.prompts import MessagesPlaceholder


def get_document_from_web(url):
  logging.getLogger("langchain_text_splitters.base").setLevel(logging.ERROR)
  loader = WebBaseLoader(url)
  docs = loader.load()
  splitter = CharacterTextSplitter(
      chunk_size=400,
      chunk_overlap=20
  )
  splitDocs = splitter.split_documents(docs)
  return splitDocs


def create_db(docs):
    embeddings = AzureOpenAIEmbeddings(
        model="text-embedding-3-large",
        azure_endpoint="https://langing.openai.azure.com/openai/deployments/Embed-test/embeddings?api-version=2023-05-15",
        openai_api_key="xxx",
        openai_api_version="2023-05-15"
    )
    vectorStore = FAISS.from_documents(docs, embeddings)
    return vectorStore


def create_chain(vectorStore):
    prompt = ChatPromptTemplate.from_messages([
        ("system", "Please answer the question based on the following context: {context}"),
        MessagesPlaceholder(variable_name="chat_history"),
        ("human", "{input}")
    ])


    chain = create_stuff_documents_chain(llm=model,
                                         prompt=prompt)

    retriever = vectorStore.as_retriever(search_kwargs={"k": 3})
    retriever_chain = create_retrieval_chain(
        retriever,
        chain
    )
    return retriever_chain


def process_chat(chain, question, chat_history):
  response = chain.invoke({
    "input": question,
    "chat_history": chat_history
  })
  return response["answer"]

chat_history = []


if __name__ == "__main__":
  docs = get_document_from_web("https://docs.smith.langchain.com/evaluation/concepts")
  vectorStore = create_db(docs)
  chain = create_chain(vectorStore)
  
  while True:
    user_input = input("You: ")
    if user_input.lower() == "exit":
        break
    response = process_chat(chain, user_input, chat_history)
    chat_history.append(HumanMessage(content=user_input))
    chat_history.append(AIMessage(content=response))
    print("Bot:", response)

Everything runs smoothly, but I don’t see any logs in Langsmith. Has anyone faced this problem before? I’ve tried everything I could think of, but it’s still not working.

Any assistance would be greatly appreciated!

Had the exact same problem when I started using Langsmith with Colab. Besides the environment variables JackWolf69 mentioned, you need to initialize the LangSmith client properly. Your code imports Client from langsmith but never actually uses it.

What fixed it for me: add os.environ['LANGCHAIN_PROJECT'] = 'your_project_name' too. Langsmith needs to know which project to log to. Also check your API key has the right permissions and you’re looking at the correct project in the dashboard.

Sometimes logs take a minute or two to show up, so wait a bit after running your code. Once those environment variables are set right, tracing should work automatically.

It appears that the code is missing the necessary Langsmith environment variables, which are crucial for enabling the logging functionality. You should set LANGCHAIN_TRACING_V2=true and provide your LANGCHAIN_API_KEY before executing any chains. In Google Colab, you can do this by adding the following lines at the beginning of your script: os.environ['LANGCHAIN_TRACING_V2'] = 'true' and os.environ['LANGCHAIN_API_KEY'] = 'your_api_key_here'. I encountered a similar issue where everything functioned correctly but no logs were captured, as Langsmith was not directed to track operations. Including these variables should rectify the problem and logging should then appear in the dashboard.