1717from dataclasses import dataclass , field , replace
1818from typing import Any , Union
1919
20- from pydantic_ai ._thinking_part import END_THINK_TAG , START_THINK_TAG
2120from pydantic_ai .exceptions import UnexpectedModelBehavior
2221from pydantic_ai .messages import (
2322 ModelResponsePart ,
@@ -72,7 +71,7 @@ def handle_text_delta(
7271 * ,
7372 vendor_part_id : VendorId | None ,
7473 content : str ,
75- extract_think_tags : bool = False ,
74+ thinking_tags : tuple [ str , str ] | None = None ,
7675 ) -> ModelResponseStreamEvent | None :
7776 """Handle incoming text content, creating or updating a TextPart in the manager as appropriate.
7877
@@ -85,7 +84,7 @@ def handle_text_delta(
8584 of text. If None, a new part will be created unless the latest part is already
8685 a TextPart.
8786 content: The text content to append to the appropriate TextPart.
88- extract_think_tags: Whether to extract `<think>` tags from the text content and handle them as thinking parts.
87+ thinking_tags: If provided, will handle content between the thinking tags as thinking parts.
8988
9089 Returns:
9190 - A `PartStartEvent` if a new part was created.
@@ -110,10 +109,10 @@ def handle_text_delta(
110109 if part_index is not None :
111110 existing_part = self ._parts [part_index ]
112111
113- if extract_think_tags and isinstance (existing_part , ThinkingPart ):
114- # We may be building a thinking part instead of a text part if we had previously seen a `<think>` tag
115- if content == END_THINK_TAG :
116- # When we see `</think>` , we're done with the thinking part and the next text delta will need a new part
112+ if thinking_tags and isinstance (existing_part , ThinkingPart ):
113+ # We may be building a thinking part instead of a text part if we had previously seen a thinking tag
114+ if content == thinking_tags [ 1 ] :
115+ # When we see the thinking end tag , we're done with the thinking part and the next text delta will need a new part
117116 self ._vendor_id_to_part_index .pop (vendor_part_id )
118117 return None
119118 else :
@@ -123,8 +122,8 @@ def handle_text_delta(
123122 else :
124123 raise UnexpectedModelBehavior (f'Cannot apply a text delta to { existing_part = } ' )
125124
126- if extract_think_tags and content == START_THINK_TAG :
127- # When we see a `<think>` tag (which is a single token), we'll build a new thinking part instead
125+ if thinking_tags and content == thinking_tags [ 0 ] :
126+ # When we see a thinking start tag (which is a single token), we'll build a new thinking part instead
128127 self ._vendor_id_to_part_index .pop (vendor_part_id , None )
129128 return self .handle_thinking_delta (vendor_part_id = vendor_part_id , content = '' )
130129
0 commit comments