@@ -83,11 +83,8 @@ async def handle_chat_completion(
8383 ]
8484 )
8585
86- # Build Langfuse metadata (tags, trace context )
86+ # Build Langfuse metadata (tags)
8787 litellm_metadata = {"tags" : tags , ** metadata }
88- if rollout_id is not None :
89- litellm_metadata ["trace_id" ] = rollout_id
90- litellm_metadata ["generation_name" ] = f"chat-{ insertion_id } "
9188
9289 langfuse_keys = config .langfuse_keys [project_id ]
9390
@@ -108,16 +105,16 @@ async def handle_chat_completion(
108105 langfuse_secret_key = langfuse_keys ["secret_key" ],
109106 )
110107
111- # Register insertion_id in Redis on success
112- if insertion_id is not None and rollout_id is not None :
113- register_insertion_id (redis_client , rollout_id , insertion_id )
114-
115108 if is_streaming :
116109 # For streaming, return a StreamingResponse with SSE format
110+ # Register insertion_id only after stream completes successfully
117111 async def stream_generator ():
118112 async for chunk in response : # type: ignore[union-attr]
119113 yield f"data: { chunk .model_dump_json ()} \n \n "
120114 yield "data: [DONE]\n \n "
115+ # Stream completed successfully - now register
116+ if insertion_id is not None and rollout_id is not None :
117+ register_insertion_id (redis_client , rollout_id , insertion_id )
121118
122119 return StreamingResponse (
123120 stream_generator (),
@@ -128,7 +125,10 @@ async def stream_generator():
128125 },
129126 )
130127 else :
131- # Non-streaming: return JSON response
128+ # Non-streaming: register insertion_id on success
129+ if insertion_id is not None and rollout_id is not None :
130+ register_insertion_id (redis_client , rollout_id , insertion_id )
131+
132132 return Response (
133133 content = response .model_dump_json (),
134134 status_code = 200 ,
0 commit comments