Tags for specific invoke of langchain #4860
-
|
Hi, First of all, thank you for the amazing tool you’ve built! Until recently, my usage of Langfuse looked like this: Lately, we’ve started using asynchronous code when invoking the LLM, like this: As a result, I’ve noticed that in certain cases of a race condition, a query is uploaded to Langfuse with the wrong tags (e.g., the query from invoke1 has the tag invoke_2). It’s clear why this happens, but I’m looking for a way to ensure the tags are set only for a specific query. What is the best way to achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
|
Shalom, @RechaviaAmit! I'm here to assist you with any bugs, questions, or contributions you have regarding Langfuse. Let's tackle this issue together! To ensure that tags are set correctly for each specific query in asynchronous environments, you should avoid modifying shared state like async def invoke1(langfuse_handler):
await chain.ainvoke(
{"input": "<user_input>"},
config={"callbacks": [langfuse_handler], "tags": ["invoke_1"]}
)
async def invoke2(langfuse_handler):
await chain.ainvoke(
{"input": "<user_input>"},
config={"callbacks": [langfuse_handler], "tags": ["invoke_2"]}
)
async def main():
from langfuse.callback import CallbackHandler
langfuse_handler = CallbackHandler(
secret_key="my_secret_key",
public_key="my_public_key",
host="my_host"
)
await invoke1(langfuse_handler)
await invoke2(langfuse_handler)By passing the tags directly in the To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
Beta Was this translation helpful? Give feedback.
-
|
This works only if the chain is runnable? |
Beta Was this translation helpful? Give feedback.
-
|
Setting tags on the handler is not recommended if you want to use different tags dynamically for each run as you will otherwise run into concurrency issues. You can set tags dynamically for each run via the langchain run config (docs): from langfuse.callback import CallbackHandler
handler = CallbackHandler()
tags = ["tag-1", "tag-2"]
chain.invoke(
{"animal": "dog"},
config={
"callbacks": [handler],
"tags": tags,
},
) |
Beta Was this translation helpful? Give feedback.
Setting tags on the handler is not recommended if you want to use different tags dynamically for each run as you will otherwise run into concurrency issues.
You can set tags dynamically for each run via the langchain run config (docs):