|
1 | 1 | from __future__ import annotations as _annotations
|
2 | 2 |
|
3 | 3 | import pytest
|
| 4 | +from inline_snapshot import snapshot |
4 | 5 |
|
5 | 6 | from pydantic_ai._thinking_part import split_content_into_text_and_thinking
|
6 |
| -from pydantic_ai.messages import ModelResponsePart, TextPart, ThinkingPart |
| 7 | +from pydantic_ai.messages import ModelResponsePart, TextPart, ThinkingPart, ThinkingPartDelta |
7 | 8 |
|
8 | 9 |
|
9 | 10 | @pytest.mark.parametrize(
|
|
26 | 27 | )
|
27 | 28 | def test_split_content_into_text_and_thinking(content: str, parts: list[ModelResponsePart]):
|
28 | 29 | assert split_content_into_text_and_thinking(content) == parts
|
| 30 | + |
| 31 | + |
| 32 | +def test_thinking_part_delta_applies_both_content_and_signature(): |
| 33 | + thinking_part = ThinkingPart(content='Initial content', signature='initial_sig') |
| 34 | + delta = ThinkingPartDelta(content_delta=' added', signature_delta='new_sig') |
| 35 | + |
| 36 | + result = delta.apply(thinking_part) |
| 37 | + |
| 38 | + # The content is appended, and the signature is updated. |
| 39 | + assert result == snapshot(ThinkingPart(content='Initial content added', signature='new_sig')) |
| 40 | + |
| 41 | + |
| 42 | +def test_thinking_part_delta_applies_signature_only(): |
| 43 | + thinking_part = ThinkingPart(content='Initial content', signature='initial_sig') |
| 44 | + delta_sig_only = ThinkingPartDelta(content_delta=None, signature_delta='sig_only') |
| 45 | + |
| 46 | + result_sig_only = delta_sig_only.apply(thinking_part) |
| 47 | + |
| 48 | + # The content is unchanged, and the signature is updated. |
| 49 | + assert result_sig_only == snapshot(ThinkingPart(content='Initial content', signature='sig_only')) |
| 50 | + |
| 51 | + |
| 52 | +def test_thinking_part_delta_applies_content_only_preserves_signature(): |
| 53 | + thinking_part = ThinkingPart(content='Initial content', signature='initial_sig') |
| 54 | + delta_content_only = ThinkingPartDelta(content_delta=' more', signature_delta=None) |
| 55 | + |
| 56 | + result_content_only = delta_content_only.apply(thinking_part) |
| 57 | + |
| 58 | + # The content is appended, and the signature is preserved. |
| 59 | + assert result_content_only == snapshot(ThinkingPart(content='Initial content more', signature='initial_sig')) |
| 60 | + |
| 61 | + |
| 62 | +def test_thinking_part_delta_applies_to_part_with_none_signature(): |
| 63 | + thinking_part_no_sig = ThinkingPart(content='No sig content', signature=None) |
| 64 | + delta_to_none_sig = ThinkingPartDelta(content_delta=' extra', signature_delta='added_sig') |
| 65 | + |
| 66 | + result_none_sig = delta_to_none_sig.apply(thinking_part_no_sig) |
| 67 | + |
| 68 | + # The content is appended, and the signature is updated. |
| 69 | + assert result_none_sig == snapshot(ThinkingPart(content='No sig content extra', signature='added_sig')) |
0 commit comments