@@ -45,37 +45,6 @@ def init(req: InitRequest):
4545 # Kick off worker thread that runs multi-turn chat via LiteLLM proxy
4646 def _worker ():
4747 try :
48- # Try to set up Langfuse trace to guarantee observability, independent of proxy wiring
49- langfuse = None
50- trace = None
51- try :
52- from langfuse import get_client # pyright: ignore[reportPrivateImportUsage]
53-
54- langfuse = get_client ()
55- id_tags = []
56- try :
57- id_tags = [
58- f"inv:{ req .metadata .get ('invocation_id' )} " ,
59- f"exp:{ req .metadata .get ('experiment_id' )} " ,
60- f"rollout:{ req .metadata .get ('rollout_id' )} " ,
61- ]
62- except Exception :
63- id_tags = []
64- trace = langfuse .api .trace .create (
65- name = "remote_chinook_rollout" ,
66- metadata = req .metadata ,
67- requester_metadata = req .metadata ,
68- tags = ["chinook_remote" , "chinook_sql" , * [t for t in id_tags if t ]],
69- input = {
70- "messages" : _clean_messages_for_api (req .messages ),
71- "tools" : req .tools ,
72- "metadata" : req .metadata ,
73- },
74- )
75- except Exception :
76- langfuse = None
77- trace = None
78-
7948 base_url = os .getenv (
8049 "LITELLM_BASE_URL" ,
8150 "https://litellm-cloud-proxy-prod-644257448872.us-central1.run.app" ,
@@ -110,48 +79,13 @@ def _worker():
11079 r .raise_for_status ()
11180 data = r .json ()
11281 assistant = data .get ("choices" , [{}])[0 ].get ("message" , {})
113- # Optionally record a generation on Langfuse
114- try :
115- if langfuse and trace and getattr (langfuse .api , "generation" , None ):
116- langfuse .api .generation .create (
117- trace_id = trace .id ,
118- name = "assistant" ,
119- input = {"messages" : _clean_messages_for_api (messages )},
120- output = assistant ,
121- )
122- except Exception :
123- pass
12482 # Append assistant for next turn
12583 messages = messages + [assistant ]
12684
127- # Update final trace output for easier adapter extraction
128- try :
129- if langfuse and trace :
130- langfuse .api .trace .update (
131- id = trace .id ,
132- output = {
133- "messages" : _clean_messages_for_api (messages ),
134- "metadata" : req .metadata ,
135- },
136- )
137- except Exception :
138- pass
139-
14085 except Exception :
14186 # Best-effort; mark as done even on error to unblock polling
14287 pass
14388 finally :
144- try :
145- if "langfuse" in locals () and langfuse is not None :
146- # Ensure buffered telemetry is sent
147- flush = getattr (langfuse , "flush" , None )
148- if callable (flush ):
149- flush ()
150- shutdown = getattr (langfuse , "shutdown" , None )
151- if callable (shutdown ):
152- shutdown ()
153- except Exception :
154- pass
15589 _STATE [req .rollout_id ]["terminated" ] = True
15690
15791 t = threading .Thread (target = _worker , daemon = True )
0 commit comments