Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions adalflow/adalflow/components/model_client/ollama_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,12 @@ def parse_chat_completion(
# Check for async generator (async streaming)
if hasattr(completion, '__aiter__'):
log.debug("Async streaming response detected")
# For streaming, return GeneratorOutput with the generator in raw_response
# This matches the OpenAI client pattern
# For streaming, wrap the generator in GeneratorOutput with proper streaming support
return GeneratorOutput(data=None, raw_response=completion, api_response=completion)
# Check for sync generator (sync streaming)
elif isinstance(completion, GeneratorType):
log.debug("Sync streaming response detected")
# For streaming, return GeneratorOutput with the generator in raw_response
# For streaming, wrap the generator in GeneratorOutput with proper streaming support
return GeneratorOutput(data=None, raw_response=completion, api_response=completion)
# Non-streaming generate API
elif self.generate:
Expand Down
32 changes: 22 additions & 10 deletions adalflow/adalflow/core/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,29 @@ def _post_call(self, completion: Any) -> GeneratorOutput:
# Now adding the data field to the output
data = output.raw_response

# TODO implement support for synchronous iterator in the future
if self.output_processors:
if data:
try:
data = self.output_processors(data)
output.data = data
except Exception as e:
log.error(f"Error processing the output processors: {e}")
output.error = str(e)
# Check if this is a streaming response (generator/iterator)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this pr does not do much. and we cant force the data to None either. It is supposed to be the final complete output data, which should be handled in ollama_client, where u have to collect all stream and save the complete one in this field. you can see example in https://github.com/SylphAI-Inc/AdalFlow/blob/main/adalflow/adalflow/components/model_client/anthropic_client.py

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep the generator not changed at all

from typing import Generator as GeneratorType
from collections.abc import AsyncGenerator as AsyncGeneratorABC

is_streaming = isinstance(data, (GeneratorType, AsyncGeneratorABC)) or hasattr(data, '__iter__') and not isinstance(data, str)

if is_streaming:
# For streaming responses, don't process with output_processors immediately
# The streaming data should be consumed by the caller
log.debug("Streaming response detected, skipping output processors")
output.data = None # Will be populated when stream is consumed
else:
output.data = data
# Non-streaming response processing
if self.output_processors:
if data:
try:
data = self.output_processors(data)
output.data = data
except Exception as e:
log.error(f"Error processing the output processors: {e}")
output.error = str(e)
else:
output.data = data

return output

Expand Down
2 changes: 2 additions & 0 deletions adalflow/tests/test_ollama_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def mock_stream_generator():
# For streaming, the parsed result should be a GeneratorOutput with raw_response containing the generator
self.assertIsInstance(parsed, GeneratorOutput)
self.assertIsNotNone(parsed.raw_response)
self.assertIsNone(parsed.data) # data should be None for streaming until consumed
self.assertEqual(parsed.api_response, result)

# Verify we can iterate through the raw_response
Expand Down Expand Up @@ -176,6 +177,7 @@ async def mock_async_stream_generator():
# For streaming, the parsed result should be a GeneratorOutput with raw_response containing the async generator
self.assertIsInstance(parsed, GeneratorOutput)
self.assertIsNotNone(parsed.raw_response)
self.assertIsNone(parsed.data) # data should be None for streaming until consumed
self.assertEqual(parsed.api_response, result)

# Verify we can iterate through the raw_response asynchronously
Expand Down
Loading