diff --git a/src/ai-bundle/src/Profiler/DataCollector.php b/src/ai-bundle/src/Profiler/DataCollector.php index 51bf91c05..23feba48c 100644 --- a/src/ai-bundle/src/Profiler/DataCollector.php +++ b/src/ai-bundle/src/Profiler/DataCollector.php @@ -155,7 +155,8 @@ private function awaitCallResults(TraceablePlatform $platform): array if (isset($platform->resultCache[$result])) { $call['result'] = $platform->resultCache[$result]; } else { - $call['result'] = $result->getContent(); + $content = $result->getContent(); + $call['result'] = $content instanceof \Generator ? null : $content; } $call['metadata'] = $result->getMetadata(); diff --git a/src/ai-bundle/tests/Profiler/DataCollectorTest.php b/src/ai-bundle/tests/Profiler/DataCollectorTest.php index 0d1595df7..9f2da1367 100644 --- a/src/ai-bundle/tests/Profiler/DataCollectorTest.php +++ b/src/ai-bundle/tests/Profiler/DataCollectorTest.php @@ -76,6 +76,30 @@ public function testCollectsDataForStreamingResponse() $this->assertSame('Assistant response', $dataCollector->getPlatformCalls()[0]['result']); } + public function testCollectsDataForUnconsumedStreamingResponse() + { + $platform = $this->createMock(PlatformInterface::class); + $traceablePlatform = new TraceablePlatform($platform); + $messageBag = new MessageBag(Message::ofUser(new Text('Hello'))); + $result = new StreamResult( + (function () { + yield 'Assistant '; + yield 'response'; + })(), + ); + + $platform->method('invoke')->willReturn(new DeferredResult(new PlainConverter($result), $this->createStub(RawResultInterface::class))); + + // Invoke but do NOT consume the stream + $traceablePlatform->invoke('gpt-4o', $messageBag, ['stream' => true]); + + $dataCollector = new DataCollector([$traceablePlatform], [], [], []); + $dataCollector->lateCollect(); + + $this->assertCount(1, $dataCollector->getPlatformCalls()); + $this->assertNull($dataCollector->getPlatformCalls()[0]['result']); + } + public function testCollectsDataForMessageStore() { $traceableMessageStore = new TraceableMessageStore(new InMemoryStore(), new MonotonicClock());