Skip to content

Commit 65a5c3e

Browse files
refactoring
1 parent 5394ce2 commit 65a5c3e

8 files changed

Lines changed: 31 additions & 37 deletions

File tree

python/packages/anthropic/agent_framework_anthropic/_chat_client.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,7 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
345345
if parsed_chunk:
346346
yield parsed_chunk
347347

348-
def _finalize(updates: Sequence[ChatResponseUpdate]) -> ChatResponse:
349-
response_format = options.get("response_format")
350-
output_format_type = response_format if isinstance(response_format, type) else None
351-
return ChatResponse.from_chat_response_updates(updates, output_format_type=output_format_type)
352-
353-
return ResponseStream(_stream(), finalizer=_finalize)
348+
return self._build_response_stream(_stream(), response_format=options.get("response_format"))
354349

355350
# Non-streaming mode
356351
async def _get_response() -> ChatResponse:

python/packages/azure-ai/agent_framework_azure_ai/_chat_client.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,7 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
362362
):
363363
yield update
364364

365-
def _finalize(updates: Sequence[ChatResponseUpdate]) -> ChatResponse:
366-
response_format = options.get("response_format")
367-
output_format_type = response_format if isinstance(response_format, type) else None
368-
return ChatResponse.from_chat_response_updates(updates, output_format_type=output_format_type)
369-
370-
return ResponseStream(_stream(), finalizer=_finalize)
365+
return self._build_response_stream(_stream(), response_format=options.get("response_format"))
371366

372367
# Non-streaming mode - collect updates and convert to response
373368
async def _get_response() -> ChatResponse:

python/packages/bedrock/agent_framework_bedrock/_chat_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
323323
raw_representation=parsed_response.raw_representation,
324324
)
325325

326-
return ResponseStream(_stream(), finalizer=ChatResponse.from_chat_response_updates)
326+
return self._build_response_stream(_stream())
327327

328328
# Non-streaming mode
329329
async def _get_response() -> ChatResponse:

python/packages/core/agent_framework/_clients.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
from abc import ABC, abstractmethod
55
from collections.abc import (
6+
AsyncIterable,
67
Awaitable,
78
Callable,
89
Mapping,
@@ -289,6 +290,28 @@ async def _validate_options(self, options: Mapping[str, Any]) -> dict[str, Any]:
289290
"""
290291
return await validate_chat_options(options)
291292

293+
def _finalize_response_updates(
294+
self,
295+
updates: Sequence[ChatResponseUpdate],
296+
*,
297+
response_format: Any | None = None,
298+
) -> ChatResponse:
299+
"""Finalize response updates into a single ChatResponse."""
300+
output_format_type = response_format if isinstance(response_format, type) else None
301+
return ChatResponse.from_chat_response_updates(updates, output_format_type=output_format_type)
302+
303+
def _build_response_stream(
304+
self,
305+
stream: AsyncIterable[ChatResponseUpdate] | Awaitable[AsyncIterable[ChatResponseUpdate]],
306+
*,
307+
response_format: Any | None = None,
308+
) -> ResponseStream[ChatResponseUpdate, ChatResponse]:
309+
"""Create a ResponseStream with the standard finalizer."""
310+
return ResponseStream(
311+
stream,
312+
finalizer=lambda updates: self._finalize_response_updates(updates, response_format=response_format),
313+
)
314+
292315
# region Internal method to be implemented by derived classes
293316

294317
@abstractmethod

python/packages/core/agent_framework/openai/_assistants_client.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,7 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
366366
async for update in self._process_stream_events(stream_obj, thread_id):
367367
yield update
368368

369-
def _finalize(updates: Sequence[ChatResponseUpdate]) -> ChatResponse:
370-
response_format = options.get("response_format")
371-
output_format_type = response_format if isinstance(response_format, type) else None
372-
return ChatResponse.from_chat_response_updates(updates, output_format_type=output_format_type)
373-
374-
return ResponseStream(_stream(), finalizer=_finalize)
369+
return self._build_response_stream(_stream(), response_format=options.get("response_format"))
375370

376371
# Non-streaming mode - collect updates and convert to response
377372
async def _get_response() -> ChatResponse:

python/packages/core/agent_framework/openai/_chat_client.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,7 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
166166
inner_exception=ex,
167167
) from ex
168168

169-
def _finalize(updates: Sequence[ChatResponseUpdate]) -> ChatResponse:
170-
response_format = options.get("response_format")
171-
output_format_type = response_format if isinstance(response_format, type) else None
172-
return ChatResponse.from_chat_response_updates(updates, output_format_type=output_format_type)
173-
174-
return ResponseStream(_stream(), finalizer=_finalize)
169+
return self._build_response_stream(_stream(), response_format=options.get("response_format"))
175170

176171
# Non-streaming mode
177172
async def _get_response() -> ChatResponse:

python/packages/core/agent_framework/openai/_responses_client.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,8 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
262262
except Exception as ex:
263263
self._handle_request_error(ex)
264264

265-
def _finalize(updates: Sequence[ChatResponseUpdate]) -> ChatResponse:
266-
response_format = validated_options.get("response_format") if validated_options else None
267-
output_format_type = response_format if isinstance(response_format, type) else None
268-
return ChatResponse.from_chat_response_updates(updates, output_format_type=output_format_type)
269-
270-
return ResponseStream(_stream(), finalizer=_finalize)
265+
response_format = validated_options.get("response_format") if validated_options else None
266+
return self._build_response_stream(_stream(), response_format=response_format)
271267

272268
# Non-streaming
273269
async def _get_response() -> ChatResponse:

python/packages/ollama/agent_framework_ollama/_chat_client.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,7 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
352352
async for part in response_object:
353353
yield self._parse_streaming_response_from_ollama(part)
354354

355-
def _finalize(updates: Sequence[ChatResponseUpdate]) -> ChatResponse:
356-
response_format = options.get("response_format")
357-
output_format_type = response_format if isinstance(response_format, type) else None
358-
return ChatResponse.from_chat_response_updates(updates, output_format_type=output_format_type)
359-
360-
return ResponseStream(_stream(), finalizer=_finalize)
355+
return self._build_response_stream(_stream(), response_format=options.get("response_format"))
361356

362357
# Non-streaming mode
363358
async def _get_response() -> ChatResponse:

0 commit comments

Comments
 (0)