Langsmith logging not working in Google Colab environment

I’m new to LangChain and having issues with Langsmith tracking in my Colab notebook. My code runs fine 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 RecursiveCharacterTextSplitter
from langchain.prompts import ChatPromptTemplate
from langchain_community.vectorstores.faiss import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain.chains import create_retrieval_chain
from langchain_core.messages import HumanMessage, AIMessage
from langchain_core.prompts import MessagesPlaceholder

def load_web_content(website_url):
    web_loader = WebBaseLoader(website_url)
    documents = web_loader.load()
    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size=500,
        chunk_overlap=50
    )
    split_documents = text_splitter.split_documents(documents)
    return split_documents

def setup_vector_store(documents):
    embedding_model = OpenAIEmbeddings(model="text-embedding-ada-002")
    vector_db = FAISS.from_documents(documents, embedding_model)
    return vector_db

def build_chat_chain(vector_db):
    system_prompt = ChatPromptTemplate.from_messages([
        ("system", "Please answer based on this context: {context}"),
        MessagesPlaceholder(variable_name="history"),
        ("user", "{input}")
    ])
    
    document_chain = create_stuff_documents_chain(
        llm=chat_model,
        prompt=system_prompt
    )
    
    search_retriever = vector_db.as_retriever(search_kwargs={"k": 2})
    final_chain = create_retrieval_chain(search_retriever, document_chain)
    return final_chain

def handle_user_message(chain, user_question, conversation_history):
    result = chain.invoke({
        "input": user_question,
        "history": conversation_history
    })
    return result["answer"]

conversation_history = []

if __name__ == "__main__":
    web_docs = load_web_content("https://docs.smith.langchain.com/")
    db = setup_vector_store(web_docs)
    chat_chain = build_chat_chain(db)
    
    while True:
        question = input("Ask: ")
        if question.lower() == "quit":
            break
        answer = handle_user_message(chat_chain, question, conversation_history)
        conversation_history.append(HumanMessage(content=question))
        conversation_history.append(AIMessage(content=answer))
        print("Answer:", answer)

The chatbot works perfectly but I don’t see any traces in Langsmith. What am I missing here? Any help would be great!

You need to initialize the LLM model with tracing enabled. Your code references chat_model but never actually defines it - that’s probably what’s breaking Langsmith’s ability to capture traces.

Add this after your environment variables:

from langchain_openai import ChatOpenAI
chat_model = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)

Make sure you’re importing and setting up tracing before creating any chains. In Colab, import order matters way more than it should. I always put all my environment setup in the first cell and run it separately - helps avoid weird timing issues with tracing.

Double-check your Langsmith project name matches exactly what’s in your dashboard. Typos will send your traces straight to nowhere.

Had this exact headache when I started using Langsmith in Colab. Your code’s solid but you’re missing the environment setup.

Set your Langsmith environment variables before running any chains. Add this at the top of your notebook:

import os
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
os.environ["LANGCHAIN_API_KEY"] = "your_api_key_here"
os.environ["LANGCHAIN_PROJECT"] = "your_project_name"

Colab doesn’t pick up environment variables like your local machine. I store my API key in Colab secrets and grab it like this:

from google.colab import userdata
os.environ["LANGCHAIN_API_KEY"] = userdata.get('LANGSMITH_API_KEY')

Restart your runtime and run the code again. Traces should start showing up in your dashboard - tracing kicks in automatically after setup.

Check your Langsmith authentication first. I hit this when my API key expired or had wrong permissions. Go to your Langsmith dashboard, regenerate a fresh API key, and update it in Colab secrets.

Colab caches imports weird sometimes. After you set environment variables, restart the runtime completely and reimport everything. Don’t just rerun cells.

Make sure your project exists in Langsmith too. If the project name in your environment variable doesn’t match an existing project, traces just vanish silently. Create the project in the dashboard first, then reference it in code. Tracing won’t auto-create projects.

colab blocks the langsmith endpoint sometimes. Try adding os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com" and check if you can reach that url from your notebook. Run !curl -I https://api.smith.langchain.com to test connectivity. If it’s blocked, you might need a different runtime or vpn.