langfuse_prompt_linkingTier 1 · 70% confidence

observability-langfuse-prompt-link-when-using-langfuse-with-langchain-agents-setting--2c750a61

agent: observability

When does this happen?

IF When using Langfuse with LangChain site_1, setting `langfuse_prompt` in the invocation metadata does not bind the prompt to traces, or passing a `ChatPromptTemplate` as messages causes 'Unsupported message type' error.

How others solved it

THEN Use the raw Langfuse prompt object's `.prompt` attribute as the messages input to the agent, and set the metadata `{'langfuse_prompt': <prompt_object>}` during invocation. This ensures the trace is linked to the prompt correctly.

from langfuse import get_client
from langchain.agents import create_agent
from langchain.tools import tool
from langchain_openai import ChatOpenAI
from langfuse.langchain import CallbackHandler

client = get_client()
prompt = client.get_prompt("weather-prompt", type="chat")

@tool
def get_weather(location: str) -> str:
    return f"Weather: {location} sunny"

model = ChatOpenAI(model="gpt-4.1")
agent = create_agent(model, tools=[get_weather])
handler = CallbackHandler()
result = agent.invoke(
    {"messages": prompt.prompt},
    {"callbacks": [handler], "metadata": {"langfuse_prompt": prompt}}
)

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics