From 7a30a719f9e76893e68f57c9d4f9287326f2d56b Mon Sep 17 00:00:00 2001 From: Tiago Freire Date: Wed, 18 Mar 2026 20:35:57 -0300 Subject: [PATCH] fix: mark batch as failed when event send fails in first-time handler The return value of _send_events_to_backend() was discarded in _initialize_backend_and_send_events, so _finalize_backend_batch was called unconditionally with the full event count even when the send returned 500. This finalized the batch as "completed" on the server while it received 0 events, producing an empty trace URL. Now check the return status and call mark_trace_batch_as_failed on 500, matching the behavior of the regular finalize_batch path. --- .../events/listeners/tracing/first_time_trace_handler.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/crewai/src/crewai/events/listeners/tracing/first_time_trace_handler.py b/lib/crewai/src/crewai/events/listeners/tracing/first_time_trace_handler.py index f9b2ad37f..56cb776a2 100644 --- a/lib/crewai/src/crewai/events/listeners/tracing/first_time_trace_handler.py +++ b/lib/crewai/src/crewai/events/listeners/tracing/first_time_trace_handler.py @@ -111,7 +111,13 @@ class FirstTimeTraceHandler: events_count = len(self.batch_manager.event_buffer) if self.batch_manager.event_buffer: - self.batch_manager._send_events_to_backend() + send_status = self.batch_manager._send_events_to_backend() + if send_status == 500 and self.batch_manager.trace_batch_id: + self.batch_manager.plus_api.mark_trace_batch_as_failed( + self.batch_manager.trace_batch_id, + "Error sending events to backend", + ) + return self.batch_manager._finalize_backend_batch(events_count) self.ephemeral_url = self.batch_manager.ephemeral_trace_url