Skip to content

Commit a554910

Browse files
committed
lift vendor part id restriction for embedded thinking - checking for coverage
1 parent 4f89122 commit a554910

File tree

7 files changed

+511
-492
lines changed

7 files changed

+511
-492
lines changed

pydantic_ai_slim/pydantic_ai/_parts_manager.py

Lines changed: 378 additions & 300 deletions
Large diffs are not rendered by default.

pydantic_ai_slim/pydantic_ai/_thinking_part.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ def split_content_into_text_and_thinking(content: str, thinking_tags: tuple[str,
2929
if content:
3030
parts.append(TextPart(content=content))
3131
return parts
32+
33+
34+
# NOTE: this utility is used by models/: `groq`, `huggingface`, `openai`, `outlines` and `tests/test_thinking_part.py`
35+
# not sure if it could be replaced by the new handling in the `_parts_manager.py` but it's worth taking a closer look.
36+
# if that's the case we could use this file to partly isolate the embedded thinking handling logic and declutter the parts manager.

pydantic_ai_slim/pydantic_ai/models/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,12 @@ async def chain_async_and_sync_iters(
640640
if end_event:
641641
yield end_event
642642

643-
self._event_iterator = iterator_with_part_end(iterator_with_final_event(self._get_event_iterator()))
643+
self._event_iterator = iterator_with_part_end(
644+
iterator_with_final_event(
645+
# TODO chain_async_and_sync_iters(iterator, self._parts_manager.final_flush())
646+
self._get_event_iterator()
647+
)
648+
)
644649
return self._event_iterator
645650

646651
@abstractmethod

pydantic_ai_slim/pydantic_ai/models/groq.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,10 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
579579
vendor_part_id='content',
580580
content=content,
581581
thinking_tags=self._model_profile.thinking_tags,
582+
# where does `ignore_leading_whitespace` come from?
583+
# `GroqModel._process_streamed_response()` returns a `GroqStreamedResponse(_model_profile=self.profile,)`
584+
# `Groq.profile`` is set at `super().__init__(settings=settings, profile=profile or provider.model_profile)`
585+
# so `_model_profile` comes either from `GroqModel(profile=...)` or `GroqModel(provider=GroqProvider(...))` where the provider infers a profile.
582586
ignore_leading_whitespace=self._model_profile.ignore_streamed_leading_whitespace,
583587
):
584588
yield event

tests/models/test_groq.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,7 +1973,6 @@ async def test_groq_model_thinking_part_iter(allow_model_requests: None, groq_ap
19731973
parts=[
19741974
ThinkingPart(
19751975
content="""\
1976-
19771976
Okay, so I want to make Uruguayan alfajores. I've heard they're a type of South American cookie sandwich with dulce de leche. I'm not entirely sure about the exact steps, but I can try to figure it out based on what I know.
19781977
19791978
First, I think alfajores are cookies, so I'll need to make the cookie part. From what I remember, the dough is probably made with flour, sugar, butter, eggs, vanilla, and maybe some baking powder or baking soda. I should look up a typical cookie dough recipe and adjust it for alfajores.
@@ -2061,8 +2060,7 @@ async def test_groq_model_thinking_part_iter(allow_model_requests: None, groq_ap
20612060

20622061
assert event_parts == snapshot(
20632062
[
2064-
PartStartEvent(index=0, part=ThinkingPart(content='\n')),
2065-
PartDeltaEvent(index=0, delta=ThinkingPartDelta(content_delta='Okay')),
2063+
PartStartEvent(index=0, part=ThinkingPart(content='Okay')),
20662064
PartDeltaEvent(index=0, delta=ThinkingPartDelta(content_delta=',')),
20672065
PartDeltaEvent(index=0, delta=ThinkingPartDelta(content_delta=' so')),
20682066
PartDeltaEvent(index=0, delta=ThinkingPartDelta(content_delta=' I')),
@@ -2582,7 +2580,6 @@ async def test_groq_model_thinking_part_iter(allow_model_requests: None, groq_ap
25822580
index=0,
25832581
part=ThinkingPart(
25842582
content="""\
2585-
25862583
Okay, so I want to make Uruguayan alfajores. I've heard they're a type of South American cookie sandwich with dulce de leche. I'm not entirely sure about the exact steps, but I can try to figure it out based on what I know.
25872584
25882585
First, I think alfajores are cookies, so I'll need to make the cookie part. From what I remember, the dough is probably made with flour, sugar, butter, eggs, vanilla, and maybe some baking powder or baking soda. I should look up a typical cookie dough recipe and adjust it for alfajores.

0 commit comments

Comments
 (0)