@@ -88,27 +88,19 @@ async def invoke_workflow_with_context(workflow: "Workflow[InputT, StreamingOutp
88
88
try :
89
89
async with workflow .run (message , observability_context = obs_context ) as runner :
90
90
result = await runner .result (to_type = to_type )
91
-
92
- # Update workflow metadata on completion
93
- if obs_context :
94
- current_workflow = obs_context .get_current_workflow ()
95
- if current_workflow :
96
- current_workflow .end_time = time .time ()
97
- current_workflow .status = "completed"
98
-
99
91
return result
100
-
101
- except Exception as e :
102
- # Update workflow metadata on failure and log error
92
+ finally :
93
+ exc = sys .exc_info ()[1 ]
103
94
if obs_context :
104
95
current_workflow = obs_context .get_current_workflow ()
105
96
if current_workflow :
106
97
current_workflow .end_time = time .time ()
107
- current_workflow .status = "failed"
108
- current_workflow .tags ["error" ] = str (e )
109
-
110
- logger .error (f"Workflow '{ workflow_name } ' failed with error: { e } " , exc_info = True )
111
- raise
98
+ if exc is None :
99
+ current_workflow .status = "completed"
100
+ else :
101
+ current_workflow .status = "failed"
102
+ current_workflow .tags ["error" ] = str (exc )
103
+ logger .error ("Workflow '%s' failed: %s" , workflow_name , exc )
112
104
113
105
@staticmethod
114
106
async def invoke_workflow_stream_with_context (
@@ -158,25 +150,18 @@ async def invoke_workflow_stream_with_context(
158
150
async with workflow .run (message , observability_context = obs_context ) as runner :
159
151
async for item in runner .result_stream (to_type = to_type ):
160
152
yield item
161
-
162
- # Update workflow metadata on completion
163
- if obs_context :
164
- current_workflow = obs_context .get_current_workflow ()
165
- if current_workflow :
166
- current_workflow .end_time = time .time ()
167
- current_workflow .status = "completed"
168
-
169
- except Exception as e :
170
- # Update workflow metadata on failure and log error
153
+ finally :
154
+ exc = sys .exc_info ()[1 ]
171
155
if obs_context :
172
156
current_workflow = obs_context .get_current_workflow ()
173
157
if current_workflow :
174
158
current_workflow .end_time = time .time ()
175
- current_workflow .status = "failed"
176
- current_workflow .tags ["error" ] = str (e )
177
-
178
- logger .error (f"Streaming workflow '{ workflow_name } ' failed with error: { e } " , exc_info = True )
179
- raise
159
+ if exc is None :
160
+ current_workflow .status = "completed"
161
+ else :
162
+ current_workflow .status = "failed"
163
+ current_workflow .tags ["error" ] = str (exc )
164
+ logger .error ("Streaming workflow '%s' failed: %s" , workflow_name , exc )
180
165
181
166
@staticmethod
182
167
def get_current_observability_context () -> Optional [ObservabilityContext ]:
@@ -268,24 +253,16 @@ async def invoke_with_steps_and_context(workflow: "Workflow[InputT, StreamingOut
268
253
to_type = to_type ,
269
254
observability_context = obs_context
270
255
)
271
-
272
- # Update workflow metadata on completion
273
- if obs_context :
274
- current_workflow = obs_context .get_current_workflow ()
275
- if current_workflow :
276
- current_workflow .end_time = time .time ()
277
- current_workflow .status = "completed"
278
-
279
256
return result , steps
280
-
281
- except Exception as e :
282
- # Update workflow metadata on failure and log error
257
+ finally :
258
+ exc = sys .exc_info ()[1 ]
283
259
if obs_context :
284
260
current_workflow = obs_context .get_current_workflow ()
285
261
if current_workflow :
286
262
current_workflow .end_time = time .time ()
287
- current_workflow .status = "failed"
288
- current_workflow .tags ["error" ] = str (e )
289
-
290
- logger .error (f"Workflow with steps '{ workflow_name } ' failed with error: { e } " , exc_info = True )
291
- raise
263
+ if exc is None :
264
+ current_workflow .status = "completed"
265
+ else :
266
+ current_workflow .status = "failed"
267
+ current_workflow .tags ["error" ] = str (exc )
268
+ logger .error ("Workflow with steps '%s' failed: %s" , workflow_name , exc )
0 commit comments