pydantic-ai-slim 0.2.20__tar.gz → 0.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pydantic-ai-slim might be problematic. Click here for more details.

Files changed (77) hide show
  1. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/PKG-INFO +7 -7
  2. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/_agent_graph.py +7 -1
  3. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/_parts_manager.py +74 -2
  4. pydantic_ai_slim-0.3.1/pydantic_ai/_thinking_part.py +36 -0
  5. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/messages.py +86 -3
  6. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/__init__.py +6 -0
  7. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/anthropic.py +53 -9
  8. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/bedrock.py +23 -4
  9. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/cohere.py +9 -1
  10. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/function.py +5 -0
  11. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/gemini.py +36 -8
  12. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/google.py +17 -2
  13. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/groq.py +13 -2
  14. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/instrumented.py +1 -1
  15. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/mistral.py +9 -1
  16. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/openai.py +84 -5
  17. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/test.py +9 -6
  18. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pyproject.toml +3 -3
  19. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/.gitignore +0 -0
  20. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/LICENSE +0 -0
  21. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/README.md +0 -0
  22. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/__init__.py +0 -0
  23. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/__main__.py +0 -0
  24. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/_a2a.py +0 -0
  25. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/_cli.py +0 -0
  26. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/_function_schema.py +0 -0
  27. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/_griffe.py +0 -0
  28. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/_output.py +0 -0
  29. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/_system_prompt.py +0 -0
  30. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/_utils.py +0 -0
  31. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/agent.py +0 -0
  32. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/common_tools/__init__.py +0 -0
  33. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/common_tools/duckduckgo.py +0 -0
  34. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/common_tools/tavily.py +0 -0
  35. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/direct.py +0 -0
  36. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/exceptions.py +0 -0
  37. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/ext/__init__.py +0 -0
  38. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/ext/langchain.py +0 -0
  39. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/format_as_xml.py +0 -0
  40. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/format_prompt.py +0 -0
  41. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/mcp.py +0 -0
  42. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/fallback.py +0 -0
  43. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/models/wrapper.py +0 -0
  44. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/__init__.py +0 -0
  45. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/_json_schema.py +0 -0
  46. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/amazon.py +0 -0
  47. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/anthropic.py +0 -0
  48. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/cohere.py +0 -0
  49. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/deepseek.py +0 -0
  50. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/google.py +0 -0
  51. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/grok.py +0 -0
  52. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/meta.py +0 -0
  53. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/mistral.py +0 -0
  54. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/openai.py +0 -0
  55. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/profiles/qwen.py +0 -0
  56. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/__init__.py +0 -0
  57. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/anthropic.py +0 -0
  58. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/azure.py +0 -0
  59. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/bedrock.py +0 -0
  60. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/cohere.py +0 -0
  61. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/deepseek.py +0 -0
  62. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/fireworks.py +0 -0
  63. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/google.py +0 -0
  64. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/google_gla.py +0 -0
  65. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/google_vertex.py +0 -0
  66. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/grok.py +0 -0
  67. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/groq.py +0 -0
  68. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/heroku.py +0 -0
  69. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/mistral.py +0 -0
  70. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/openai.py +0 -0
  71. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/openrouter.py +0 -0
  72. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/providers/together.py +0 -0
  73. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/py.typed +0 -0
  74. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/result.py +0 -0
  75. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/settings.py +0 -0
  76. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/tools.py +0 -0
  77. {pydantic_ai_slim-0.2.20 → pydantic_ai_slim-0.3.1}/pydantic_ai/usage.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pydantic-ai-slim
3
- Version: 0.2.20
3
+ Version: 0.3.1
4
4
  Summary: Agent Framework / shim to use Pydantic with LLMs, slim package
5
5
  Author-email: Samuel Colvin <samuel@pydantic.dev>, Marcelo Trylesinski <marcelotryle@gmail.com>, David Montague <david@pydantic.dev>, Alex Hall <alex@pydantic.dev>
6
6
  License-Expression: MIT
@@ -30,15 +30,15 @@ Requires-Dist: exceptiongroup; python_version < '3.11'
30
30
  Requires-Dist: griffe>=1.3.2
31
31
  Requires-Dist: httpx>=0.27
32
32
  Requires-Dist: opentelemetry-api>=1.28.0
33
- Requires-Dist: pydantic-graph==0.2.20
33
+ Requires-Dist: pydantic-graph==0.3.1
34
34
  Requires-Dist: pydantic>=2.10
35
35
  Requires-Dist: typing-inspection>=0.4.0
36
36
  Provides-Extra: a2a
37
- Requires-Dist: fasta2a==0.2.20; extra == 'a2a'
37
+ Requires-Dist: fasta2a==0.3.1; extra == 'a2a'
38
38
  Provides-Extra: anthropic
39
39
  Requires-Dist: anthropic>=0.52.0; extra == 'anthropic'
40
40
  Provides-Extra: bedrock
41
- Requires-Dist: boto3>=1.35.74; extra == 'bedrock'
41
+ Requires-Dist: boto3>=1.37.24; extra == 'bedrock'
42
42
  Provides-Extra: cli
43
43
  Requires-Dist: argcomplete>=3.5.0; extra == 'cli'
44
44
  Requires-Dist: prompt-toolkit>=3; extra == 'cli'
@@ -48,11 +48,11 @@ Requires-Dist: cohere>=5.13.11; (platform_system != 'Emscripten') and extra == '
48
48
  Provides-Extra: duckduckgo
49
49
  Requires-Dist: duckduckgo-search>=7.0.0; extra == 'duckduckgo'
50
50
  Provides-Extra: evals
51
- Requires-Dist: pydantic-evals==0.2.20; extra == 'evals'
51
+ Requires-Dist: pydantic-evals==0.3.1; extra == 'evals'
52
52
  Provides-Extra: google
53
53
  Requires-Dist: google-genai>=1.15.0; extra == 'google'
54
54
  Provides-Extra: groq
55
- Requires-Dist: groq>=0.15.0; extra == 'groq'
55
+ Requires-Dist: groq>=0.19.0; extra == 'groq'
56
56
  Provides-Extra: logfire
57
57
  Requires-Dist: logfire>=3.11.0; extra == 'logfire'
58
58
  Provides-Extra: mcp
@@ -60,7 +60,7 @@ Requires-Dist: mcp>=1.9.4; (python_version >= '3.10') and extra == 'mcp'
60
60
  Provides-Extra: mistral
61
61
  Requires-Dist: mistralai>=1.2.5; extra == 'mistral'
62
62
  Provides-Extra: openai
63
- Requires-Dist: openai>=1.75.0; extra == 'openai'
63
+ Requires-Dist: openai>=1.76.0; extra == 'openai'
64
64
  Provides-Extra: tavily
65
65
  Requires-Dist: tavily-python>=0.5.0; extra == 'tavily'
66
66
  Provides-Extra: vertexai
@@ -442,7 +442,7 @@ class CallToolsNode(AgentNode[DepsT, NodeRunEndT]):
442
442
  async for _event in stream:
443
443
  pass
444
444
 
445
- async def _run_stream(
445
+ async def _run_stream( # noqa: C901
446
446
  self, ctx: GraphRunContext[GraphAgentState, GraphAgentDeps[DepsT, NodeRunEndT]]
447
447
  ) -> AsyncIterator[_messages.HandleResponseEvent]:
448
448
  if self._events_iterator is None:
@@ -458,6 +458,12 @@ class CallToolsNode(AgentNode[DepsT, NodeRunEndT]):
458
458
  texts.append(part.content)
459
459
  elif isinstance(part, _messages.ToolCallPart):
460
460
  tool_calls.append(part)
461
+ elif isinstance(part, _messages.ThinkingPart):
462
+ # We don't need to do anything with thinking parts in this tool-calling node.
463
+ # We need to handle text parts in case there are no tool calls and/or the desired output comes
464
+ # from the text, but thinking parts should not directly influence the execution of tools or
465
+ # determination of the next node of graph execution here.
466
+ pass
461
467
  else:
462
468
  assert_never(part)
463
469
 
@@ -25,6 +25,8 @@ from pydantic_ai.messages import (
25
25
  PartStartEvent,
26
26
  TextPart,
27
27
  TextPartDelta,
28
+ ThinkingPart,
29
+ ThinkingPartDelta,
28
30
  ToolCallPart,
29
31
  ToolCallPartDelta,
30
32
  )
@@ -86,8 +88,7 @@ class ModelResponsePartsManager:
86
88
  A `PartStartEvent` if a new part was created, or a `PartDeltaEvent` if an existing part was updated.
87
89
 
88
90
  Raises:
89
- UnexpectedModelBehavior: If attempting to apply text content to a part that is
90
- not a TextPart.
91
+ UnexpectedModelBehavior: If attempting to apply text content to a part that is not a TextPart.
91
92
  """
92
93
  existing_text_part_and_index: tuple[TextPart, int] | None = None
93
94
 
@@ -122,6 +123,77 @@ class ModelResponsePartsManager:
122
123
  self._parts[part_index] = part_delta.apply(existing_text_part)
123
124
  return PartDeltaEvent(index=part_index, delta=part_delta)
124
125
 
126
+ def handle_thinking_delta(
127
+ self,
128
+ *,
129
+ vendor_part_id: Hashable | None,
130
+ content: str | None = None,
131
+ signature: str | None = None,
132
+ ) -> ModelResponseStreamEvent:
133
+ """Handle incoming thinking content, creating or updating a ThinkingPart in the manager as appropriate.
134
+
135
+ When `vendor_part_id` is None, the latest part is updated if it exists and is a ThinkingPart;
136
+ otherwise, a new ThinkingPart is created. When a non-None ID is specified, the ThinkingPart corresponding
137
+ to that vendor ID is either created or updated.
138
+
139
+ Args:
140
+ vendor_part_id: The ID the vendor uses to identify this piece
141
+ of thinking. If None, a new part will be created unless the latest part is already
142
+ a ThinkingPart.
143
+ content: The thinking content to append to the appropriate ThinkingPart.
144
+ signature: An optional signature for the thinking content.
145
+
146
+ Returns:
147
+ A `PartStartEvent` if a new part was created, or a `PartDeltaEvent` if an existing part was updated.
148
+
149
+ Raises:
150
+ UnexpectedModelBehavior: If attempting to apply a thinking delta to a part that is not a ThinkingPart.
151
+ """
152
+ existing_thinking_part_and_index: tuple[ThinkingPart, int] | None = None
153
+
154
+ if vendor_part_id is None:
155
+ # If the vendor_part_id is None, check if the latest part is a ThinkingPart to update
156
+ if self._parts:
157
+ part_index = len(self._parts) - 1
158
+ latest_part = self._parts[part_index]
159
+ if isinstance(latest_part, ThinkingPart): # pragma: no branch
160
+ existing_thinking_part_and_index = latest_part, part_index
161
+ else:
162
+ # Otherwise, attempt to look up an existing ThinkingPart by vendor_part_id
163
+ part_index = self._vendor_id_to_part_index.get(vendor_part_id)
164
+ if part_index is not None:
165
+ existing_part = self._parts[part_index]
166
+ if not isinstance(existing_part, ThinkingPart):
167
+ raise UnexpectedModelBehavior(f'Cannot apply a thinking delta to {existing_part=}')
168
+ existing_thinking_part_and_index = existing_part, part_index
169
+
170
+ if existing_thinking_part_and_index is None:
171
+ if content is not None:
172
+ # There is no existing thinking part that should be updated, so create a new one
173
+ new_part_index = len(self._parts)
174
+ part = ThinkingPart(content=content, signature=signature)
175
+ if vendor_part_id is not None: # pragma: no branch
176
+ self._vendor_id_to_part_index[vendor_part_id] = new_part_index
177
+ self._parts.append(part)
178
+ return PartStartEvent(index=new_part_index, part=part)
179
+ else:
180
+ raise UnexpectedModelBehavior('Cannot create a ThinkingPart with no content')
181
+ else:
182
+ if content is not None:
183
+ # Update the existing ThinkingPart with the new content delta
184
+ existing_thinking_part, part_index = existing_thinking_part_and_index
185
+ part_delta = ThinkingPartDelta(content_delta=content)
186
+ self._parts[part_index] = part_delta.apply(existing_thinking_part)
187
+ return PartDeltaEvent(index=part_index, delta=part_delta)
188
+ elif signature is not None:
189
+ # Update the existing ThinkingPart with the new signature delta
190
+ existing_thinking_part, part_index = existing_thinking_part_and_index
191
+ part_delta = ThinkingPartDelta(signature_delta=signature)
192
+ self._parts[part_index] = part_delta.apply(existing_thinking_part)
193
+ return PartDeltaEvent(index=part_index, delta=part_delta)
194
+ else:
195
+ raise UnexpectedModelBehavior('Cannot update a ThinkingPart with no content or signature')
196
+
125
197
  def handle_tool_call_delta(
126
198
  self,
127
199
  *,
@@ -0,0 +1,36 @@
1
+ from __future__ import annotations as _annotations
2
+
3
+ from pydantic_ai.messages import TextPart, ThinkingPart
4
+
5
+ START_THINK_TAG = '<think>'
6
+ END_THINK_TAG = '</think>'
7
+
8
+
9
+ def split_content_into_text_and_thinking(content: str) -> list[ThinkingPart | TextPart]:
10
+ """Split a string into text and thinking parts.
11
+
12
+ Some models don't return the thinking part as a separate part, but rather as a tag in the content.
13
+ This function splits the content into text and thinking parts.
14
+
15
+ We use the `<think>` tag because that's how Groq uses it in the `raw` format, so instead of using `<Thinking>` or
16
+ something else, we just match the tag to make it easier for other models that don't support the `ThinkingPart`.
17
+ """
18
+ parts: list[ThinkingPart | TextPart] = []
19
+
20
+ start_index = content.find(START_THINK_TAG)
21
+ while start_index >= 0:
22
+ before_think, content = content[:start_index], content[start_index + len(START_THINK_TAG) :]
23
+ if before_think:
24
+ parts.append(TextPart(content=before_think))
25
+ end_index = content.find(END_THINK_TAG)
26
+ if end_index >= 0:
27
+ think_content, content = content[:end_index], content[end_index + len(END_THINK_TAG) :]
28
+ parts.append(ThinkingPart(content=think_content))
29
+ else:
30
+ # We lose the `<think>` tag, but it shouldn't matter.
31
+ parts.append(TextPart(content=content))
32
+ content = ''
33
+ start_index = content.find(START_THINK_TAG)
34
+ if content:
35
+ parts.append(TextPart(content=content))
36
+ return parts
@@ -14,7 +14,10 @@ from opentelemetry._events import Event # pyright: ignore[reportPrivateImportUs
14
14
  from typing_extensions import TypeAlias
15
15
 
16
16
  from . import _utils
17
- from ._utils import generate_tool_call_id as _generate_tool_call_id, now_utc as _now_utc
17
+ from ._utils import (
18
+ generate_tool_call_id as _generate_tool_call_id,
19
+ now_utc as _now_utc,
20
+ )
18
21
  from .exceptions import UnexpectedModelBehavior
19
22
  from .usage import Usage
20
23
 
@@ -531,6 +534,32 @@ class TextPart:
531
534
  __repr__ = _utils.dataclasses_no_defaults_repr
532
535
 
533
536
 
537
+ @dataclass(repr=False)
538
+ class ThinkingPart:
539
+ """A thinking response from a model."""
540
+
541
+ content: str
542
+ """The thinking content of the response."""
543
+
544
+ id: str | None = None
545
+ """The identifier of the thinking part."""
546
+
547
+ signature: str | None = None
548
+ """The signature of the thinking.
549
+
550
+ The signature is only available on the Anthropic models.
551
+ """
552
+
553
+ part_kind: Literal['thinking'] = 'thinking'
554
+ """Part type identifier, this is available on all parts as a discriminator."""
555
+
556
+ def has_content(self) -> bool:
557
+ """Return `True` if the thinking content is non-empty."""
558
+ return bool(self.content) # pragma: no cover
559
+
560
+ __repr__ = _utils.dataclasses_no_defaults_repr
561
+
562
+
534
563
  @dataclass(repr=False)
535
564
  class ToolCallPart:
536
565
  """A tool call from a model."""
@@ -589,7 +618,7 @@ class ToolCallPart:
589
618
  __repr__ = _utils.dataclasses_no_defaults_repr
590
619
 
591
620
 
592
- ModelResponsePart = Annotated[Union[TextPart, ToolCallPart], pydantic.Discriminator('part_kind')]
621
+ ModelResponsePart = Annotated[Union[TextPart, ToolCallPart, ThinkingPart], pydantic.Discriminator('part_kind')]
593
622
  """A message part returned by a model."""
594
623
 
595
624
 
@@ -699,6 +728,58 @@ class TextPartDelta:
699
728
  __repr__ = _utils.dataclasses_no_defaults_repr
700
729
 
701
730
 
731
+ @dataclass(repr=False)
732
+ class ThinkingPartDelta:
733
+ """A partial update (delta) for a `ThinkingPart` to append new thinking content."""
734
+
735
+ content_delta: str | None = None
736
+ """The incremental thinking content to add to the existing `ThinkingPart` content."""
737
+
738
+ signature_delta: str | None = None
739
+ """Optional signature delta.
740
+
741
+ Note this is never treated as a delta — it can replace None.
742
+ """
743
+
744
+ part_delta_kind: Literal['thinking'] = 'thinking'
745
+ """Part delta type identifier, used as a discriminator."""
746
+
747
+ @overload
748
+ def apply(self, part: ModelResponsePart) -> ThinkingPart: ...
749
+
750
+ @overload
751
+ def apply(self, part: ModelResponsePart | ThinkingPartDelta) -> ThinkingPart | ThinkingPartDelta: ...
752
+
753
+ def apply(self, part: ModelResponsePart | ThinkingPartDelta) -> ThinkingPart | ThinkingPartDelta:
754
+ """Apply this thinking delta to an existing `ThinkingPart`.
755
+
756
+ Args:
757
+ part: The existing model response part, which must be a `ThinkingPart`.
758
+
759
+ Returns:
760
+ A new `ThinkingPart` with updated thinking content.
761
+
762
+ Raises:
763
+ ValueError: If `part` is not a `ThinkingPart`.
764
+ """
765
+ if isinstance(part, ThinkingPart):
766
+ new_content = part.content + self.content_delta if self.content_delta else part.content
767
+ new_signature = self.signature_delta if self.signature_delta is not None else part.signature
768
+ return replace(part, content=new_content, signature=new_signature)
769
+ elif isinstance(part, ThinkingPartDelta):
770
+ if self.content_delta is None and self.signature_delta is None:
771
+ raise ValueError('Cannot apply ThinkingPartDelta with no content or signature')
772
+ if self.signature_delta is not None:
773
+ return replace(part, signature_delta=self.signature_delta)
774
+ if self.content_delta is not None:
775
+ return replace(part, content_delta=self.content_delta)
776
+ raise ValueError( # pragma: no cover
777
+ f'Cannot apply ThinkingPartDeltas to non-ThinkingParts or non-ThinkingPartDeltas ({part=}, {self=})'
778
+ )
779
+
780
+ __repr__ = _utils.dataclasses_no_defaults_repr
781
+
782
+
702
783
  @dataclass(repr=False)
703
784
  class ToolCallPartDelta:
704
785
  """A partial update (delta) for a `ToolCallPart` to modify tool name, arguments, or tool call ID."""
@@ -818,7 +899,9 @@ class ToolCallPartDelta:
818
899
  __repr__ = _utils.dataclasses_no_defaults_repr
819
900
 
820
901
 
821
- ModelResponsePartDelta = Annotated[Union[TextPartDelta, ToolCallPartDelta], pydantic.Discriminator('part_delta_kind')]
902
+ ModelResponsePartDelta = Annotated[
903
+ Union[TextPartDelta, ThinkingPartDelta, ToolCallPartDelta], pydantic.Discriminator('part_delta_kind')
904
+ ]
822
905
  """A partial update (delta) for any model response part."""
823
906
 
824
907
 
@@ -139,8 +139,11 @@ KnownModelName = TypeAliasType(
139
139
  'google-gla:gemini-2.0-flash-lite-preview-02-05',
140
140
  'google-gla:gemini-2.0-pro-exp-02-05',
141
141
  'google-gla:gemini-2.5-flash-preview-05-20',
142
+ 'google-gla:gemini-2.5-flash',
143
+ 'google-gla:gemini-2.5-flash-lite-preview-06-17',
142
144
  'google-gla:gemini-2.5-pro-exp-03-25',
143
145
  'google-gla:gemini-2.5-pro-preview-05-06',
146
+ 'google-gla:gemini-2.5-pro',
144
147
  'google-vertex:gemini-1.5-flash',
145
148
  'google-vertex:gemini-1.5-flash-8b',
146
149
  'google-vertex:gemini-1.5-pro',
@@ -149,8 +152,11 @@ KnownModelName = TypeAliasType(
149
152
  'google-vertex:gemini-2.0-flash-lite-preview-02-05',
150
153
  'google-vertex:gemini-2.0-pro-exp-02-05',
151
154
  'google-vertex:gemini-2.5-flash-preview-05-20',
155
+ 'google-vertex:gemini-2.5-flash',
156
+ 'google-vertex:gemini-2.5-flash-lite-preview-06-17',
152
157
  'google-vertex:gemini-2.5-pro-exp-03-25',
153
158
  'google-vertex:gemini-2.5-pro-preview-05-06',
159
+ 'google-vertex:gemini-2.5-pro',
154
160
  'gpt-3.5-turbo',
155
161
  'gpt-3.5-turbo-0125',
156
162
  'gpt-3.5-turbo-0301',
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations as _annotations
2
2
 
3
3
  import io
4
+ import warnings
4
5
  from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator
5
6
  from contextlib import asynccontextmanager
6
7
  from dataclasses import dataclass, field
@@ -23,6 +24,7 @@ from ..messages import (
23
24
  RetryPromptPart,
24
25
  SystemPromptPart,
25
26
  TextPart,
27
+ ThinkingPart,
26
28
  ToolCallPart,
27
29
  ToolReturnPart,
28
30
  UserPromptPart,
@@ -52,9 +54,15 @@ try:
52
54
  BetaRawMessageStartEvent,
53
55
  BetaRawMessageStopEvent,
54
56
  BetaRawMessageStreamEvent,
57
+ BetaRedactedThinkingBlock,
58
+ BetaSignatureDelta,
55
59
  BetaTextBlock,
56
60
  BetaTextBlockParam,
57
61
  BetaTextDelta,
62
+ BetaThinkingBlock,
63
+ BetaThinkingBlockParam,
64
+ BetaThinkingConfigParam,
65
+ BetaThinkingDelta,
58
66
  BetaToolChoiceParam,
59
67
  BetaToolParam,
60
68
  BetaToolResultBlockParam,
@@ -90,7 +98,14 @@ class AnthropicModelSettings(ModelSettings, total=False):
90
98
  anthropic_metadata: BetaMetadataParam
91
99
  """An object describing metadata about the request.
92
100
 
93
- Contains `user_id`, an external identifier for the user who is associated with the request."""
101
+ Contains `user_id`, an external identifier for the user who is associated with the request.
102
+ """
103
+
104
+ anthropic_thinking: BetaThinkingConfigParam
105
+ """Determine whether the model should generate a thinking block.
106
+
107
+ See [the Anthropic docs](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking) for more information.
108
+ """
94
109
 
95
110
 
96
111
  @dataclass(init=False)
@@ -227,6 +242,7 @@ class AnthropicModel(Model):
227
242
  tools=tools or NOT_GIVEN,
228
243
  tool_choice=tool_choice or NOT_GIVEN,
229
244
  stream=stream,
245
+ thinking=model_settings.get('anthropic_thinking', NOT_GIVEN),
230
246
  stop_sequences=model_settings.get('stop_sequences', NOT_GIVEN),
231
247
  temperature=model_settings.get('temperature', NOT_GIVEN),
232
248
  top_p=model_settings.get('top_p', NOT_GIVEN),
@@ -246,6 +262,14 @@ class AnthropicModel(Model):
246
262
  for item in response.content:
247
263
  if isinstance(item, BetaTextBlock):
248
264
  items.append(TextPart(content=item.text))
265
+ elif isinstance(item, BetaRedactedThinkingBlock): # pragma: no cover
266
+ warnings.warn(
267
+ 'PydanticAI currently does not handle redacted thinking blocks. '
268
+ 'If you have a suggestion on how we should handle them, please open an issue.',
269
+ UserWarning,
270
+ )
271
+ elif isinstance(item, BetaThinkingBlock):
272
+ items.append(ThinkingPart(content=item.thinking, signature=item.signature))
249
273
  else:
250
274
  assert isinstance(item, BetaToolUseBlock), f'unexpected item type {type(item)}'
251
275
  items.append(
@@ -312,11 +336,21 @@ class AnthropicModel(Model):
312
336
  if len(user_content_params) > 0:
313
337
  anthropic_messages.append(BetaMessageParam(role='user', content=user_content_params))
314
338
  elif isinstance(m, ModelResponse):
315
- assistant_content_params: list[BetaTextBlockParam | BetaToolUseBlockParam] = []
339
+ assistant_content_params: list[BetaTextBlockParam | BetaToolUseBlockParam | BetaThinkingBlockParam] = []
316
340
  for response_part in m.parts:
317
341
  if isinstance(response_part, TextPart):
318
342
  if response_part.content: # Only add non-empty text
319
343
  assistant_content_params.append(BetaTextBlockParam(text=response_part.content, type='text'))
344
+ elif isinstance(response_part, ThinkingPart):
345
+ # NOTE: We don't send ThinkingPart to the providers yet. If you are unsatisfied with this,
346
+ # please open an issue. The below code is the code to send thinking to the provider.
347
+ # assert response_part.signature is not None, 'Thinking part must have a signature'
348
+ # assistant_content_params.append(
349
+ # BetaThinkingBlockParam(
350
+ # thinking=response_part.content, signature=response_part.signature, type='thinking'
351
+ # )
352
+ # )
353
+ pass
320
354
  else:
321
355
  tool_use_block_param = BetaToolUseBlockParam(
322
356
  id=_guard_tool_call_id(t=response_part),
@@ -445,10 +479,14 @@ class AnthropicStreamedResponse(StreamedResponse):
445
479
  if isinstance(event, BetaRawContentBlockStartEvent):
446
480
  current_block = event.content_block
447
481
  if isinstance(current_block, BetaTextBlock) and current_block.text:
448
- yield self._parts_manager.handle_text_delta( # pragma: lax no cover
449
- vendor_part_id='content', content=current_block.text
482
+ yield self._parts_manager.handle_text_delta(vendor_part_id='content', content=current_block.text)
483
+ elif isinstance(current_block, BetaThinkingBlock):
484
+ yield self._parts_manager.handle_thinking_delta(
485
+ vendor_part_id='thinking',
486
+ content=current_block.thinking,
487
+ signature=current_block.signature,
450
488
  )
451
- elif isinstance(current_block, BetaToolUseBlock): # pragma: no branch
489
+ elif isinstance(current_block, BetaToolUseBlock):
452
490
  maybe_event = self._parts_manager.handle_tool_call_delta(
453
491
  vendor_part_id=current_block.id,
454
492
  tool_name=current_block.name,
@@ -460,14 +498,20 @@ class AnthropicStreamedResponse(StreamedResponse):
460
498
 
461
499
  elif isinstance(event, BetaRawContentBlockDeltaEvent):
462
500
  if isinstance(event.delta, BetaTextDelta):
463
- yield self._parts_manager.handle_text_delta( # pragma: no cover
464
- vendor_part_id='content', content=event.delta.text
501
+ yield self._parts_manager.handle_text_delta(vendor_part_id='content', content=event.delta.text)
502
+ elif isinstance(event.delta, BetaThinkingDelta):
503
+ yield self._parts_manager.handle_thinking_delta(
504
+ vendor_part_id='thinking', content=event.delta.thinking
505
+ )
506
+ elif isinstance(event.delta, BetaSignatureDelta):
507
+ yield self._parts_manager.handle_thinking_delta(
508
+ vendor_part_id='thinking', signature=event.delta.signature
465
509
  )
466
- elif ( # pragma: no branch
510
+ elif (
467
511
  current_block
468
512
  and event.delta.type == 'input_json_delta'
469
513
  and isinstance(current_block, BetaToolUseBlock)
470
- ):
514
+ ): # pragma: no branch
471
515
  maybe_event = self._parts_manager.handle_tool_call_delta(
472
516
  vendor_part_id=current_block.id,
473
517
  tool_name='',
@@ -2,6 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  import functools
4
4
  import typing
5
+ import warnings
5
6
  from collections.abc import AsyncIterator, Iterable, Iterator, Mapping
6
7
  from contextlib import asynccontextmanager
7
8
  from dataclasses import dataclass, field
@@ -27,6 +28,7 @@ from pydantic_ai.messages import (
27
28
  RetryPromptPart,
28
29
  SystemPromptPart,
29
30
  TextPart,
31
+ ThinkingPart,
30
32
  ToolCallPart,
31
33
  ToolReturnPart,
32
34
  UserPromptPart,
@@ -265,11 +267,16 @@ class BedrockConverseModel(Model):
265
267
  items: list[ModelResponsePart] = []
266
268
  if message := response['output'].get('message'): # pragma: no branch
267
269
  for item in message['content']:
270
+ if reasoning_content := item.get('reasoningContent'):
271
+ reasoning_text = reasoning_content.get('reasoningText')
272
+ if reasoning_text: # pragma: no branch
273
+ thinking_part = ThinkingPart(content=reasoning_text['text'])
274
+ if reasoning_signature := reasoning_text.get('signature'):
275
+ thinking_part.signature = reasoning_signature
276
+ items.append(thinking_part)
268
277
  if text := item.get('text'):
269
278
  items.append(TextPart(content=text))
270
- else:
271
- tool_use = item.get('toolUse')
272
- assert tool_use is not None, f'Found a content that is not a text or tool use: {item}'
279
+ elif tool_use := item.get('toolUse'):
273
280
  items.append(
274
281
  ToolCallPart(
275
282
  tool_name=tool_use['name'],
@@ -385,7 +392,7 @@ class BedrockConverseModel(Model):
385
392
 
386
393
  return tool_config
387
394
 
388
- async def _map_messages(
395
+ async def _map_messages( # noqa: C901
389
396
  self, messages: list[ModelMessage]
390
397
  ) -> tuple[list[SystemContentBlockTypeDef], list[MessageUnionTypeDef]]:
391
398
  """Maps a `pydantic_ai.Message` to the Bedrock `MessageUnionTypeDef`.
@@ -448,6 +455,9 @@ class BedrockConverseModel(Model):
448
455
  for item in message.parts:
449
456
  if isinstance(item, TextPart):
450
457
  content.append({'text': item.content})
458
+ elif isinstance(item, ThinkingPart):
459
+ # NOTE: We don't pass the thinking part to Bedrock since it raises an error.
460
+ pass
451
461
  else:
452
462
  assert isinstance(item, ToolCallPart)
453
463
  content.append(self._map_tool_call(item))
@@ -592,6 +602,15 @@ class BedrockStreamedResponse(StreamedResponse):
592
602
  if 'contentBlockDelta' in chunk:
593
603
  index = chunk['contentBlockDelta']['contentBlockIndex']
594
604
  delta = chunk['contentBlockDelta']['delta']
605
+ if 'reasoningContent' in delta:
606
+ if text := delta['reasoningContent'].get('text'):
607
+ yield self._parts_manager.handle_thinking_delta(vendor_part_id=index, content=text)
608
+ else: # pragma: no cover
609
+ warnings.warn(
610
+ f'Only text reasoning content is supported yet, but you got {delta["reasoningContent"]}. '
611
+ 'Please report this to the maintainers.',
612
+ UserWarning,
613
+ )
595
614
  if 'text' in delta:
596
615
  yield self._parts_manager.handle_text_delta(vendor_part_id=index, content=delta['text'])
597
616
  if 'toolUse' in delta:
@@ -6,6 +6,8 @@ from typing import Literal, Union, cast
6
6
 
7
7
  from typing_extensions import assert_never
8
8
 
9
+ from pydantic_ai._thinking_part import split_content_into_text_and_thinking
10
+
9
11
  from .. import ModelHTTPError, usage
10
12
  from .._utils import generate_tool_call_id as _generate_tool_call_id, guard_tool_call_id as _guard_tool_call_id
11
13
  from ..messages import (
@@ -16,6 +18,7 @@ from ..messages import (
16
18
  RetryPromptPart,
17
19
  SystemPromptPart,
18
20
  TextPart,
21
+ ThinkingPart,
19
22
  ToolCallPart,
20
23
  ToolReturnPart,
21
24
  UserPromptPart,
@@ -187,7 +190,7 @@ class CohereModel(Model):
187
190
  # While Cohere's API returns a list, it only does that for future proofing
188
191
  # and currently only one item is being returned.
189
192
  choice = response.message.content[0]
190
- parts.append(TextPart(choice.text))
193
+ parts.extend(split_content_into_text_and_thinking(choice.text))
191
194
  for c in response.message.tool_calls or []:
192
195
  if c.function and c.function.name and c.function.arguments: # pragma: no branch
193
196
  parts.append(
@@ -211,6 +214,11 @@ class CohereModel(Model):
211
214
  for item in message.parts:
212
215
  if isinstance(item, TextPart):
213
216
  texts.append(item.content)
217
+ elif isinstance(item, ThinkingPart):
218
+ # NOTE: We don't send ThinkingPart to the providers yet. If you are unsatisfied with this,
219
+ # please open an issue. The below code is the code to send thinking to the provider.
220
+ # texts.append(f'<think>\n{item.content}\n</think>')
221
+ pass
214
222
  elif isinstance(item, ToolCallPart):
215
223
  tool_calls.append(self._map_tool_call(item))
216
224
  else:
@@ -24,6 +24,7 @@ from ..messages import (
24
24
  RetryPromptPart,
25
25
  SystemPromptPart,
26
26
  TextPart,
27
+ ThinkingPart,
27
28
  ToolCallPart,
28
29
  ToolReturnPart,
29
30
  UserContent,
@@ -268,6 +269,10 @@ def _estimate_usage(messages: Iterable[ModelMessage]) -> usage.Usage:
268
269
  for part in message.parts:
269
270
  if isinstance(part, TextPart):
270
271
  response_tokens += _estimate_string_tokens(part.content)
272
+ elif isinstance(part, ThinkingPart):
273
+ # NOTE: We don't send ThinkingPart to the providers yet.
274
+ # If you are unsatisfied with this, please open an issue.
275
+ pass
271
276
  elif isinstance(part, ToolCallPart):
272
277
  call = part
273
278
  response_tokens += 1 + _estimate_string_tokens(call.args_as_json_str())