How to Set Custom Trace ID with a Decorator and Include langfuse_observation_id? #3545
-
I’m trying to set a custom trace ID using the @observe() decorator and pass langfuse_observation_id into the trace context. Here's a snippet of my code:
I want to know how to correctly pass the langfuse_observation_id in this context. What’s the best approach? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
To set a custom trace ID and pass @observe()
async def _agenerate(
self,
messages: str | list[Any],
streaming: bool = True,
callbacks: list[BaseLLMCallback] | None = None,
langfuse_observation_id: str = None,
**kwargs: Any,
) -> str:
extra_body = kwargs.pop('extra_body', self.extra_body)
if extra_body and 'metadata' in extra_body:
trace_user_id = extra_body['metadata'].get('trace_user_id')
trace_id = extra_body['metadata'].get('trace_id')
langfuse_context.update_current_trace(
user_id=trace_user_id,
)
# Pass langfuse_observation_id to the async client call if needed
response = await self.async_client.chat.completions.create(
model=model,
messages=messages,
stream=streaming,
langfuse_observation_id=langfuse_observation_id,
**kwargs,
) By adding |
Beta Was this translation helpful? Give feedback.
-
Hi @octadion, what is this This is the recommended way to set custom ids on traces created via the decorator: https://langfuse.com/docs/sdk/python/decorators#set-custom-ids |
Beta Was this translation helpful? Give feedback.
Hi @octadion, what is this
AsyncCompletions
class that disallows passing additional kwargs?This is the recommended way to set custom ids on traces created via the decorator: https://langfuse.com/docs/sdk/python/decorators#set-custom-ids