Skip to content

Commit 967e612

Browse files
committed
Refactor OpenAI and OpenRouter models to use Iterable
Replaced Generator with Iterable in several methods to improve compatibility and maintainability.
1 parent e32f767 commit 967e612

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

pydantic_ai_slim/pydantic_ai/models/openai.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import itertools
55
import json
66
import warnings
7-
from collections.abc import AsyncIterable, AsyncIterator, Generator, Iterable, Sequence
7+
from collections.abc import AsyncIterable, AsyncIterator, Iterable, Sequence
88
from contextlib import asynccontextmanager
99
from dataclasses import dataclass, field, replace
1010
from datetime import datetime
@@ -611,7 +611,7 @@ def _process_response(self, response: chat.ChatCompletion | str) -> ModelRespons
611611
finish_reason=self._map_finish_reason(choice.finish_reason),
612612
)
613613

614-
def _process_thinking(self, message: chat.ChatCompletionMessage) -> Generator[ThinkingPart]:
614+
def _process_thinking(self, message: chat.ChatCompletionMessage) -> Iterable[ThinkingPart]:
615615
"""Hook that maps reasoning tokens to thinking parts.
616616
617617
This method may be overridden by subclasses of `OpenAIChatModel` to apply custom mappings.
@@ -627,7 +627,7 @@ def _process_thinking(self, message: chat.ChatCompletionMessage) -> Generator[Th
627627
if reasoning := getattr(message, 'reasoning', None):
628628
yield ThinkingPart(id='reasoning', content=reasoning, provider_name=self.system)
629629

630-
def _process_content(self, message: chat.ChatCompletionMessage) -> Generator[TextPart | ThinkingPart]:
630+
def _process_content(self, message: chat.ChatCompletionMessage) -> Iterable[TextPart | ThinkingPart]:
631631
"""Hook that maps the message content to thinking or text parts.
632632
633633
This method may be overridden by subclasses of `OpenAIChatModel` to apply custom mappings.
@@ -636,7 +636,7 @@ def _process_content(self, message: chat.ChatCompletionMessage) -> Generator[Tex
636636
for part in split_content_into_text_and_thinking(message.content, self.profile.thinking_tags):
637637
yield replace(part, id='content', provider_name=self.system) if isinstance(part, ThinkingPart) else part
638638

639-
def _process_tool_calls(self, message: chat.ChatCompletionMessage) -> Generator[ToolCallPart]:
639+
def _process_tool_calls(self, message: chat.ChatCompletionMessage) -> Iterable[ToolCallPart]:
640640
"""Hook that maps tool calls to tool call parts.
641641
642642
This method may be overridden by subclasses of `OpenAIChatModel` to apply custom mappings.

pydantic_ai_slim/pydantic_ai/models/openrouter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations as _annotations
22

33
import itertools
4-
from collections.abc import Generator, Iterable
4+
from collections.abc import Iterable
55
from dataclasses import dataclass, field
66
from typing import Annotated, Any, Literal, TypeAlias, cast
77

@@ -572,7 +572,7 @@ def _validate_completion(self, response: chat.ChatCompletion) -> _OpenRouterChat
572572
return response
573573

574574
@override
575-
def _process_thinking(self, message: chat.ChatCompletionMessage) -> Generator[ThinkingPart]:
575+
def _process_thinking(self, message: chat.ChatCompletionMessage) -> Iterable[ThinkingPart]:
576576
assert isinstance(message, _OpenRouterCompletionMessage)
577577

578578
if reasoning_details := message.reasoning_details:
@@ -581,7 +581,7 @@ def _process_thinking(self, message: chat.ChatCompletionMessage) -> Generator[Th
581581
else:
582582
yield from super()._process_thinking(message)
583583

584-
def _process_image(self, message: chat.ChatCompletionMessage) -> Generator[FilePart]:
584+
def _process_image(self, message: chat.ChatCompletionMessage) -> Iterable[FilePart]:
585585
assert isinstance(message, _OpenRouterCompletionMessage)
586586

587587
if images := message.images:

0 commit comments

Comments
 (0)