pydantic-ai-slim 0.2.15__tar.gz → 0.2.17__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 (76) hide show
  1. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/PKG-INFO +5 -5
  2. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/_agent_graph.py +0 -4
  3. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/_function_schema.py +4 -4
  4. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/_output.py +1 -1
  5. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/_utils.py +5 -1
  6. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/agent.py +5 -6
  7. pydantic_ai_slim-0.2.17/pydantic_ai/ext/langchain.py +61 -0
  8. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/mcp.py +57 -15
  9. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/messages.py +43 -13
  10. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/__init__.py +95 -3
  11. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/anthropic.py +3 -11
  12. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/bedrock.py +23 -15
  13. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/gemini.py +18 -14
  14. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/google.py +12 -11
  15. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/groq.py +4 -4
  16. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/instrumented.py +98 -32
  17. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/mistral.py +5 -5
  18. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/openai.py +56 -42
  19. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/openai.py +9 -2
  20. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/__init__.py +5 -1
  21. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/google_vertex.py +1 -1
  22. pydantic_ai_slim-0.2.17/pydantic_ai/providers/heroku.py +82 -0
  23. pydantic_ai_slim-0.2.17/pydantic_ai/py.typed +0 -0
  24. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/settings.py +1 -0
  25. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/tools.py +53 -6
  26. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pyproject.toml +1 -1
  27. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/.gitignore +0 -0
  28. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/LICENSE +0 -0
  29. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/README.md +0 -0
  30. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/__init__.py +0 -0
  31. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/__main__.py +0 -0
  32. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/_a2a.py +0 -0
  33. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/_cli.py +0 -0
  34. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/_griffe.py +0 -0
  35. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/_parts_manager.py +0 -0
  36. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/_system_prompt.py +0 -0
  37. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/common_tools/__init__.py +0 -0
  38. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/common_tools/duckduckgo.py +0 -0
  39. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/common_tools/tavily.py +0 -0
  40. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/direct.py +0 -0
  41. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/exceptions.py +0 -0
  42. /pydantic_ai_slim-0.2.15/pydantic_ai/py.typed → /pydantic_ai_slim-0.2.17/pydantic_ai/ext/__init__.py +0 -0
  43. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/format_as_xml.py +0 -0
  44. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/format_prompt.py +0 -0
  45. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/cohere.py +0 -0
  46. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/fallback.py +0 -0
  47. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/function.py +0 -0
  48. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/test.py +0 -0
  49. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/models/wrapper.py +0 -0
  50. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/__init__.py +0 -0
  51. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/_json_schema.py +0 -0
  52. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/amazon.py +0 -0
  53. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/anthropic.py +0 -0
  54. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/cohere.py +0 -0
  55. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/deepseek.py +0 -0
  56. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/google.py +0 -0
  57. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/grok.py +0 -0
  58. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/meta.py +0 -0
  59. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/mistral.py +0 -0
  60. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/profiles/qwen.py +0 -0
  61. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/anthropic.py +0 -0
  62. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/azure.py +0 -0
  63. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/bedrock.py +0 -0
  64. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/cohere.py +0 -0
  65. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/deepseek.py +0 -0
  66. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/fireworks.py +0 -0
  67. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/google.py +0 -0
  68. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/google_gla.py +0 -0
  69. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/grok.py +0 -0
  70. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/groq.py +0 -0
  71. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/mistral.py +0 -0
  72. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/openai.py +0 -0
  73. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/openrouter.py +0 -0
  74. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/providers/together.py +0 -0
  75. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/pydantic_ai/result.py +0 -0
  76. {pydantic_ai_slim-0.2.15 → pydantic_ai_slim-0.2.17}/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.15
3
+ Version: 0.2.17
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,11 +30,11 @@ 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.15
33
+ Requires-Dist: pydantic-graph==0.2.17
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.15; extra == 'a2a'
37
+ Requires-Dist: fasta2a==0.2.17; extra == 'a2a'
38
38
  Provides-Extra: anthropic
39
39
  Requires-Dist: anthropic>=0.52.0; extra == 'anthropic'
40
40
  Provides-Extra: bedrock
@@ -48,7 +48,7 @@ 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.15; extra == 'evals'
51
+ Requires-Dist: pydantic-evals==0.2.17; extra == 'evals'
52
52
  Provides-Extra: google
53
53
  Requires-Dist: google-genai>=1.15.0; extra == 'google'
54
54
  Provides-Extra: groq
@@ -56,7 +56,7 @@ Requires-Dist: groq>=0.15.0; extra == 'groq'
56
56
  Provides-Extra: logfire
57
57
  Requires-Dist: logfire>=3.11.0; extra == 'logfire'
58
58
  Provides-Extra: mcp
59
- Requires-Dist: mcp>=1.9.0; (python_version >= '3.10') and extra == 'mcp'
59
+ Requires-Dist: mcp>=1.9.2; (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
@@ -151,10 +151,6 @@ class UserPromptNode(AgentNode[DepsT, NodeRunEndT]):
151
151
  ctx.state.message_history = history
152
152
  run_context.messages = history
153
153
 
154
- # TODO: We need to make it so that function_tools are not shared between runs
155
- # See comment on the current_retry field of `Tool` for more details.
156
- for tool in ctx.deps.function_tools.values():
157
- tool.current_retry = 0
158
154
  return next_message
159
155
 
160
156
  async def _prepare_messages(
@@ -7,7 +7,7 @@ from __future__ import annotations as _annotations
7
7
 
8
8
  import inspect
9
9
  from collections.abc import Awaitable
10
- from dataclasses import dataclass
10
+ from dataclasses import dataclass, field
11
11
  from inspect import Parameter, signature
12
12
  from typing import TYPE_CHECKING, Any, Callable, cast
13
13
 
@@ -43,9 +43,9 @@ class FunctionSchema:
43
43
  # if not None, the function takes a single by that name (besides potentially `info`)
44
44
  takes_ctx: bool
45
45
  is_async: bool
46
- single_arg_name: str | None
47
- positional_fields: list[str]
48
- var_positional_field: str | None
46
+ single_arg_name: str | None = None
47
+ positional_fields: list[str] = field(default_factory=list)
48
+ var_positional_field: str | None = None
49
49
 
50
50
  async def call(self, args_dict: dict[str, Any], ctx: RunContext[Any]) -> Any:
51
51
  args, kwargs = self._call_args(args_dict, ctx)
@@ -138,7 +138,7 @@ class ToolOutput(Generic[OutputDataT]):
138
138
  T_co = TypeVar('T_co', covariant=True)
139
139
  # output_type=Type or output_type=function or output_type=object.method
140
140
  SimpleOutputType = TypeAliasType(
141
- 'SimpleOutputType', Union[type[T_co], Callable[..., T_co], Callable[..., Awaitable[T_co]]], type_params=(T_co,)
141
+ 'SimpleOutputType', Union[type[T_co], Callable[..., Union[Awaitable[T_co], T_co]]], type_params=(T_co,)
142
142
  )
143
143
  # output_type=ToolOutput(<see above>) or <see above>
144
144
  SimpleOutputTypeOrMarker = TypeAliasType(
@@ -12,7 +12,7 @@ from types import GenericAlias
12
12
  from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar, Union
13
13
 
14
14
  from anyio.to_thread import run_sync
15
- from pydantic import BaseModel
15
+ from pydantic import BaseModel, TypeAdapter
16
16
  from pydantic.json_schema import JsonSchemaValue
17
17
  from typing_extensions import ParamSpec, TypeAlias, TypeGuard, is_typeddict
18
18
 
@@ -298,3 +298,7 @@ def dataclasses_no_defaults_repr(self: Any) -> str:
298
298
  f'{f.name}={getattr(self, f.name)!r}' for f in fields(self) if f.repr and getattr(self, f.name) != f.default
299
299
  )
300
300
  return f'{self.__class__.__qualname__}({", ".join(kv_pairs)})'
301
+
302
+
303
+ def number_to_datetime(x: int | float) -> datetime:
304
+ return TypeAdapter(datetime).validate_python(x)
@@ -646,11 +646,6 @@ class Agent(Generic[AgentDepsT, OutputDataT]):
646
646
  # typecast reasonable, even though it is possible to violate it with otherwise-type-checked code.
647
647
  output_validators = cast(list[_output.OutputValidator[AgentDepsT, RunOutputDataT]], self._output_validators)
648
648
 
649
- # TODO: Instead of this, copy the function tools to ensure they don't share current_retry state between agent
650
- # runs. Requires some changes to `Tool` to make them copyable though.
651
- for v in self._function_tools.values():
652
- v.current_retry = 0
653
-
654
649
  model_settings = merge_model_settings(self.model_settings, model_settings)
655
650
  usage_limits = usage_limits or _usage.UsageLimits()
656
651
 
@@ -679,6 +674,10 @@ class Agent(Generic[AgentDepsT, OutputDataT]):
679
674
  instructions += '\n' + await instructions_runner.run(run_context)
680
675
  return instructions.strip()
681
676
 
677
+ # Copy the function tools so that retry state is agent-run-specific
678
+ # Note that the retry count is reset to 0 when this happens due to the `default=0` and `init=False`.
679
+ run_function_tools = {k: dataclasses.replace(v) for k, v in self._function_tools.items()}
680
+
682
681
  graph_deps = _agent_graph.GraphAgentDeps[AgentDepsT, RunOutputDataT](
683
682
  user_deps=deps,
684
683
  prompt=user_prompt,
@@ -690,7 +689,7 @@ class Agent(Generic[AgentDepsT, OutputDataT]):
690
689
  end_strategy=self.end_strategy,
691
690
  output_schema=output_schema,
692
691
  output_validators=output_validators,
693
- function_tools=self._function_tools,
692
+ function_tools=run_function_tools,
694
693
  mcp_servers=self._mcp_servers,
695
694
  default_retries=self._default_retries,
696
695
  tracer=tracer,
@@ -0,0 +1,61 @@
1
+ from typing import Any, Protocol
2
+
3
+ from pydantic.json_schema import JsonSchemaValue
4
+
5
+ from pydantic_ai.tools import Tool
6
+
7
+
8
+ class LangChainTool(Protocol):
9
+ # args are like
10
+ # {'dir_path': {'default': '.', 'description': 'Subdirectory to search in.', 'title': 'Dir Path', 'type': 'string'},
11
+ # 'pattern': {'description': 'Unix shell regex, where * matches everything.', 'title': 'Pattern', 'type': 'string'}}
12
+ @property
13
+ def args(self) -> dict[str, JsonSchemaValue]: ...
14
+
15
+ def get_input_jsonschema(self) -> JsonSchemaValue: ...
16
+
17
+ @property
18
+ def name(self) -> str: ...
19
+
20
+ @property
21
+ def description(self) -> str: ...
22
+
23
+ def run(self, *args: Any, **kwargs: Any) -> str: ...
24
+
25
+
26
+ __all__ = ('tool_from_langchain',)
27
+
28
+
29
+ def tool_from_langchain(langchain_tool: LangChainTool) -> Tool:
30
+ """Creates a Pydantic tool proxy from a LangChain tool.
31
+
32
+ Args:
33
+ langchain_tool: The LangChain tool to wrap.
34
+
35
+ Returns:
36
+ A Pydantic tool that corresponds to the LangChain tool.
37
+ """
38
+ function_name = langchain_tool.name
39
+ function_description = langchain_tool.description
40
+ inputs = langchain_tool.args.copy()
41
+ required = sorted({name for name, detail in inputs.items() if 'default' not in detail})
42
+ schema: JsonSchemaValue = langchain_tool.get_input_jsonschema()
43
+ if 'additionalProperties' not in schema:
44
+ schema['additionalProperties'] = False
45
+ if required:
46
+ schema['required'] = required
47
+
48
+ defaults = {name: detail['default'] for name, detail in inputs.items() if 'default' in detail}
49
+
50
+ # restructures the arguments to match langchain tool run
51
+ def proxy(*args: Any, **kwargs: Any) -> str:
52
+ assert not args, 'This should always be called with kwargs'
53
+ kwargs = defaults | kwargs
54
+ return langchain_tool.run(kwargs)
55
+
56
+ return Tool.from_schema(
57
+ function=proxy,
58
+ name=function_name,
59
+ description=function_description,
60
+ json_schema=schema,
61
+ )
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import base64
4
+ import functools
4
5
  import json
5
6
  from abc import ABC, abstractmethod
6
7
  from collections.abc import AsyncIterator, Sequence
@@ -11,12 +12,13 @@ from types import TracebackType
11
12
  from typing import Any
12
13
 
13
14
  import anyio
15
+ import httpx
14
16
  from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
17
+ from mcp.shared.message import SessionMessage
15
18
  from mcp.types import (
16
19
  BlobResourceContents,
17
20
  EmbeddedResource,
18
21
  ImageContent,
19
- JSONRPCMessage,
20
22
  LoggingLevel,
21
23
  TextContent,
22
24
  TextResourceContents,
@@ -56,8 +58,8 @@ class MCPServer(ABC):
56
58
  """
57
59
 
58
60
  _client: ClientSession
59
- _read_stream: MemoryObjectReceiveStream[JSONRPCMessage | Exception]
60
- _write_stream: MemoryObjectSendStream[JSONRPCMessage]
61
+ _read_stream: MemoryObjectReceiveStream[SessionMessage | Exception]
62
+ _write_stream: MemoryObjectSendStream[SessionMessage]
61
63
  _exit_stack: AsyncExitStack
62
64
 
63
65
  @abstractmethod
@@ -66,8 +68,8 @@ class MCPServer(ABC):
66
68
  self,
67
69
  ) -> AsyncIterator[
68
70
  tuple[
69
- MemoryObjectReceiveStream[JSONRPCMessage | Exception],
70
- MemoryObjectSendStream[JSONRPCMessage],
71
+ MemoryObjectReceiveStream[SessionMessage | Exception],
72
+ MemoryObjectSendStream[SessionMessage],
71
73
  ]
72
74
  ]:
73
75
  """Create the streams for the MCP server."""
@@ -266,8 +268,8 @@ class MCPServerStdio(MCPServer):
266
268
  self,
267
269
  ) -> AsyncIterator[
268
270
  tuple[
269
- MemoryObjectReceiveStream[JSONRPCMessage | Exception],
270
- MemoryObjectSendStream[JSONRPCMessage],
271
+ MemoryObjectReceiveStream[SessionMessage | Exception],
272
+ MemoryObjectSendStream[SessionMessage],
271
273
  ]
272
274
  ]:
273
275
  server = StdioServerParameters(command=self.command, args=list(self.args), env=self.env, cwd=self.cwd)
@@ -326,6 +328,31 @@ class MCPServerHTTP(MCPServer):
326
328
 
327
329
  These headers will be passed directly to the underlying `httpx.AsyncClient`.
328
330
  Useful for authentication, custom headers, or other HTTP-specific configurations.
331
+
332
+ !!! note
333
+ You can either pass `headers` or `http_client`, but not both.
334
+
335
+ See [`MCPServerHTTP.http_client`][pydantic_ai.mcp.MCPServerHTTP.http_client] for more information.
336
+ """
337
+
338
+ http_client: httpx.AsyncClient | None = None
339
+ """An `httpx.AsyncClient` to use with the SSE endpoint.
340
+
341
+ This client may be configured to use customized connection parameters like self-signed certificates.
342
+
343
+ !!! note
344
+ You can either pass `headers` or `http_client`, but not both.
345
+
346
+ If you want to use both, you can pass the headers to the `http_client` instead:
347
+
348
+ ```python {py="3.10"}
349
+ import httpx
350
+
351
+ from pydantic_ai.mcp import MCPServerHTTP
352
+
353
+ http_client = httpx.AsyncClient(headers={'Authorization': 'Bearer ...'})
354
+ server = MCPServerHTTP('http://localhost:3001/sse', http_client=http_client)
355
+ ```
329
356
  """
330
357
 
331
358
  timeout: float = 5
@@ -362,18 +389,33 @@ class MCPServerHTTP(MCPServer):
362
389
  async def client_streams(
363
390
  self,
364
391
  ) -> AsyncIterator[
365
- tuple[
366
- MemoryObjectReceiveStream[JSONRPCMessage | Exception],
367
- MemoryObjectSendStream[JSONRPCMessage],
368
- ]
392
+ tuple[MemoryObjectReceiveStream[SessionMessage | Exception], MemoryObjectSendStream[SessionMessage]]
369
393
  ]: # pragma: no cover
370
- async with sse_client(
394
+ if self.http_client and self.headers:
395
+ raise ValueError('`http_client` is mutually exclusive with `headers`.')
396
+
397
+ sse_client_partial = functools.partial(
398
+ sse_client,
371
399
  url=self.url,
372
- headers=self.headers,
373
400
  timeout=self.timeout,
374
401
  sse_read_timeout=self.sse_read_timeout,
375
- ) as (read_stream, write_stream):
376
- yield read_stream, write_stream
402
+ )
403
+
404
+ if self.http_client is not None:
405
+
406
+ def httpx_client_factory(
407
+ headers: dict[str, str] | None = None,
408
+ timeout: httpx.Timeout | None = None,
409
+ auth: httpx.Auth | None = None,
410
+ ) -> httpx.AsyncClient:
411
+ assert self.http_client is not None
412
+ return self.http_client
413
+
414
+ async with sse_client_partial(httpx_client_factory=httpx_client_factory) as (read_stream, write_stream):
415
+ yield read_stream, write_stream
416
+ else:
417
+ async with sse_client_partial(headers=self.headers) as (read_stream, write_stream):
418
+ yield read_stream, write_stream
377
419
 
378
420
  def _get_log_level(self) -> LoggingLevel | None:
379
421
  return self.log_level
@@ -2,6 +2,7 @@ from __future__ import annotations as _annotations
2
2
 
3
3
  import base64
4
4
  import uuid
5
+ from abc import ABC, abstractmethod
5
6
  from collections.abc import Sequence
6
7
  from dataclasses import dataclass, field, replace
7
8
  from datetime import datetime
@@ -80,8 +81,35 @@ class SystemPromptPart:
80
81
 
81
82
 
82
83
  @dataclass(repr=False)
83
- class VideoUrl:
84
- """A URL to an video."""
84
+ class FileUrl(ABC):
85
+ """Abstract base class for any URL-based file."""
86
+
87
+ url: str
88
+ """The URL of the file."""
89
+
90
+ force_download: bool = False
91
+ """If the model supports it:
92
+
93
+ * If True, the file is downloaded and the data is sent to the model as bytes.
94
+ * If False, the URL is sent directly to the model and no download is performed.
95
+ """
96
+
97
+ @property
98
+ @abstractmethod
99
+ def media_type(self) -> str:
100
+ """Return the media type of the file, based on the url."""
101
+
102
+ @property
103
+ @abstractmethod
104
+ def format(self) -> str:
105
+ """The file format."""
106
+
107
+ __repr__ = _utils.dataclasses_no_defaults_repr
108
+
109
+
110
+ @dataclass(repr=False)
111
+ class VideoUrl(FileUrl):
112
+ """A URL to a video."""
85
113
 
86
114
  url: str
87
115
  """The URL of the video."""
@@ -108,9 +136,19 @@ class VideoUrl:
108
136
  return 'video/x-ms-wmv'
109
137
  elif self.url.endswith('.three_gp'):
110
138
  return 'video/3gpp'
139
+ # Assume that YouTube videos are mp4 because there would be no extension
140
+ # to infer from. This should not be a problem, as Gemini disregards media
141
+ # type for YouTube URLs.
142
+ elif self.is_youtube:
143
+ return 'video/mp4'
111
144
  else:
112
145
  raise ValueError(f'Unknown video file extension: {self.url}')
113
146
 
147
+ @property
148
+ def is_youtube(self) -> bool:
149
+ """True if the URL has a YouTube domain."""
150
+ return self.url.startswith(('https://youtu.be/', 'https://youtube.com/', 'https://www.youtube.com/'))
151
+
114
152
  @property
115
153
  def format(self) -> VideoFormat:
116
154
  """The file format of the video.
@@ -119,11 +157,9 @@ class VideoUrl:
119
157
  """
120
158
  return _video_format_lookup[self.media_type]
121
159
 
122
- __repr__ = _utils.dataclasses_no_defaults_repr
123
-
124
160
 
125
161
  @dataclass(repr=False)
126
- class AudioUrl:
162
+ class AudioUrl(FileUrl):
127
163
  """A URL to an audio file."""
128
164
 
129
165
  url: str
@@ -147,11 +183,9 @@ class AudioUrl:
147
183
  """The file format of the audio file."""
148
184
  return _audio_format_lookup[self.media_type]
149
185
 
150
- __repr__ = _utils.dataclasses_no_defaults_repr
151
-
152
186
 
153
187
  @dataclass(repr=False)
154
- class ImageUrl:
188
+ class ImageUrl(FileUrl):
155
189
  """A URL to an image."""
156
190
 
157
191
  url: str
@@ -182,11 +216,9 @@ class ImageUrl:
182
216
  """
183
217
  return _image_format_lookup[self.media_type]
184
218
 
185
- __repr__ = _utils.dataclasses_no_defaults_repr
186
-
187
219
 
188
220
  @dataclass(repr=False)
189
- class DocumentUrl:
221
+ class DocumentUrl(FileUrl):
190
222
  """The URL of the document."""
191
223
 
192
224
  url: str
@@ -215,8 +247,6 @@ class DocumentUrl:
215
247
  except KeyError as e:
216
248
  raise ValueError(f'Unknown document media type: {media_type}') from e
217
249
 
218
- __repr__ = _utils.dataclasses_no_defaults_repr
219
-
220
250
 
221
251
  @dataclass(repr=False)
222
252
  class BinaryContent:
@@ -6,21 +6,23 @@ specific LLM being used.
6
6
 
7
7
  from __future__ import annotations as _annotations
8
8
 
9
+ import base64
9
10
  from abc import ABC, abstractmethod
10
11
  from collections.abc import AsyncIterator, Iterator
11
12
  from contextlib import asynccontextmanager, contextmanager
12
13
  from dataclasses import dataclass, field, replace
13
14
  from datetime import datetime
14
15
  from functools import cache, cached_property
16
+ from typing import Generic, TypeVar, overload
15
17
 
16
18
  import httpx
17
- from typing_extensions import Literal, TypeAliasType
19
+ from typing_extensions import Literal, TypeAliasType, TypedDict
18
20
 
19
21
  from pydantic_ai.profiles import DEFAULT_PROFILE, ModelProfile, ModelProfileSpec
20
22
 
21
23
  from .._parts_manager import ModelResponsePartsManager
22
24
  from ..exceptions import UserError
23
- from ..messages import ModelMessage, ModelRequest, ModelResponse, ModelResponseStreamEvent
25
+ from ..messages import FileUrl, ModelMessage, ModelRequest, ModelResponse, ModelResponseStreamEvent, VideoUrl
24
26
  from ..profiles._json_schema import JsonSchemaTransformer
25
27
  from ..settings import ModelSettings
26
28
  from ..tools import ToolDefinition
@@ -211,6 +213,11 @@ KnownModelName = TypeAliasType(
211
213
  'groq:llama-3.2-3b-preview',
212
214
  'groq:llama-3.2-11b-vision-preview',
213
215
  'groq:llama-3.2-90b-vision-preview',
216
+ 'heroku:claude-3-5-haiku',
217
+ 'heroku:claude-3-5-sonnet-latest',
218
+ 'heroku:claude-3-7-sonnet',
219
+ 'heroku:claude-4-sonnet',
220
+ 'heroku:claude-3-haiku',
214
221
  'mistral:codestral-latest',
215
222
  'mistral:mistral-large-latest',
216
223
  'mistral:mistral-moderation-latest',
@@ -543,7 +550,7 @@ def infer_model(model: Model | KnownModelName | str) -> Model:
543
550
  from .cohere import CohereModel
544
551
 
545
552
  return CohereModel(model_name, provider=provider)
546
- elif provider in ('openai', 'deepseek', 'azure', 'openrouter', 'grok', 'fireworks', 'together'):
553
+ elif provider in ('openai', 'deepseek', 'azure', 'openrouter', 'grok', 'fireworks', 'together', 'heroku'):
547
554
  from .openai import OpenAIModel
548
555
 
549
556
  return OpenAIModel(model_name, provider=provider)
@@ -606,6 +613,91 @@ def _cached_async_http_transport() -> httpx.AsyncHTTPTransport:
606
613
  return httpx.AsyncHTTPTransport()
607
614
 
608
615
 
616
+ DataT = TypeVar('DataT', str, bytes)
617
+
618
+
619
+ class DownloadedItem(TypedDict, Generic[DataT]):
620
+ """The downloaded data and its type."""
621
+
622
+ data: DataT
623
+ """The downloaded data."""
624
+
625
+ data_type: str
626
+ """The type of data that was downloaded.
627
+
628
+ Extracted from header "content-type", but defaults to the media type inferred from the file URL if content-type is "application/octet-stream".
629
+ """
630
+
631
+
632
+ @overload
633
+ async def download_item(
634
+ item: FileUrl,
635
+ data_format: Literal['bytes'],
636
+ type_format: Literal['mime', 'extension'] = 'mime',
637
+ ) -> DownloadedItem[bytes]: ...
638
+
639
+
640
+ @overload
641
+ async def download_item(
642
+ item: FileUrl,
643
+ data_format: Literal['base64', 'base64_uri', 'text'],
644
+ type_format: Literal['mime', 'extension'] = 'mime',
645
+ ) -> DownloadedItem[str]: ...
646
+
647
+
648
+ async def download_item(
649
+ item: FileUrl,
650
+ data_format: Literal['bytes', 'base64', 'base64_uri', 'text'] = 'bytes',
651
+ type_format: Literal['mime', 'extension'] = 'mime',
652
+ ) -> DownloadedItem[str] | DownloadedItem[bytes]:
653
+ """Download an item by URL and return the content as a bytes object or a (base64-encoded) string.
654
+
655
+ Args:
656
+ item: The item to download.
657
+ data_format: The format to return the content in:
658
+ - `bytes`: The raw bytes of the content.
659
+ - `base64`: The base64-encoded content.
660
+ - `base64_uri`: The base64-encoded content as a data URI.
661
+ - `text`: The content as a string.
662
+ type_format: The format to return the media type in:
663
+ - `mime`: The media type as a MIME type.
664
+ - `extension`: The media type as an extension.
665
+
666
+ Raises:
667
+ UserError: If the URL points to a YouTube video or its protocol is gs://.
668
+ """
669
+ if item.url.startswith('gs://'):
670
+ raise UserError('Downloading from protocol "gs://" is not supported.')
671
+ elif isinstance(item, VideoUrl) and item.is_youtube:
672
+ raise UserError('Downloading YouTube videos is not supported.')
673
+
674
+ client = cached_async_http_client()
675
+ response = await client.get(item.url, follow_redirects=True)
676
+ response.raise_for_status()
677
+
678
+ if content_type := response.headers.get('content-type'):
679
+ content_type = content_type.split(';')[0]
680
+ if content_type == 'application/octet-stream':
681
+ content_type = None
682
+
683
+ media_type = content_type or item.media_type
684
+
685
+ data_type = media_type
686
+ if type_format == 'extension':
687
+ data_type = data_type.split('/')[1]
688
+
689
+ data = response.content
690
+ if data_format in ('base64', 'base64_uri'):
691
+ data = base64.b64encode(data).decode('utf-8')
692
+ if data_format == 'base64_uri':
693
+ data = f'data:{media_type};base64,{data}'
694
+ return DownloadedItem[str](data=data, data_type=data_type)
695
+ elif data_format == 'text':
696
+ return DownloadedItem[str](data=data.decode('utf-8'), data_type=data_type)
697
+ else:
698
+ return DownloadedItem[bytes](data=data, data_type=data_type)
699
+
700
+
609
701
  @cache
610
702
  def get_user_agent() -> str:
611
703
  """Get the user agent string for the HTTP client."""
@@ -31,14 +31,7 @@ from ..profiles import ModelProfileSpec
31
31
  from ..providers import Provider, infer_provider
32
32
  from ..settings import ModelSettings
33
33
  from ..tools import ToolDefinition
34
- from . import (
35
- Model,
36
- ModelRequestParameters,
37
- StreamedResponse,
38
- cached_async_http_client,
39
- check_allow_model_requests,
40
- get_user_agent,
41
- )
34
+ from . import Model, ModelRequestParameters, StreamedResponse, check_allow_model_requests, download_item, get_user_agent
42
35
 
43
36
  try:
44
37
  from anthropic import NOT_GIVEN, APIStatusError, AsyncAnthropic, AsyncStream
@@ -372,11 +365,10 @@ class AnthropicModel(Model):
372
365
  if item.media_type == 'application/pdf':
373
366
  yield BetaBase64PDFBlockParam(source={'url': item.url, 'type': 'url'}, type='document')
374
367
  elif item.media_type == 'text/plain':
375
- response = await cached_async_http_client().get(item.url)
376
- response.raise_for_status()
368
+ downloaded_item = await download_item(item, data_format='text')
377
369
  yield BetaBase64PDFBlockParam(
378
370
  source=BetaPlainTextSourceParam(
379
- data=response.text, media_type=item.media_type, type='text'
371
+ data=downloaded_item['data'], media_type=item.media_type, type='text'
380
372
  ),
381
373
  type='document',
382
374
  )
@@ -32,12 +32,7 @@ from pydantic_ai.messages import (
32
32
  UserPromptPart,
33
33
  VideoUrl,
34
34
  )
35
- from pydantic_ai.models import (
36
- Model,
37
- ModelRequestParameters,
38
- StreamedResponse,
39
- cached_async_http_client,
40
- )
35
+ from pydantic_ai.models import Model, ModelRequestParameters, StreamedResponse, download_item
41
36
  from pydantic_ai.profiles import ModelProfileSpec
42
37
  from pydantic_ai.providers import Provider, infer_provider
43
38
  from pydantic_ai.providers.bedrock import BedrockModelProfile
@@ -55,6 +50,7 @@ if TYPE_CHECKING:
55
50
  ConverseResponseTypeDef,
56
51
  ConverseStreamMetadataEventTypeDef,
57
52
  ConverseStreamOutputTypeDef,
53
+ DocumentBlockTypeDef,
58
54
  GuardrailConfigurationTypeDef,
59
55
  ImageBlockTypeDef,
60
56
  InferenceConfigurationTypeDef,
@@ -507,25 +503,37 @@ class BedrockConverseModel(Model):
507
503
  else:
508
504
  raise NotImplementedError('Binary content is not supported yet.')
509
505
  elif isinstance(item, (ImageUrl, DocumentUrl, VideoUrl)):
510
- response = await cached_async_http_client().get(item.url)
511
- response.raise_for_status()
506
+ downloaded_item = await download_item(item, data_format='bytes', type_format='extension')
507
+ format = downloaded_item['data_type']
512
508
  if item.kind == 'image-url':
513
509
  format = item.media_type.split('/')[1]
514
510
  assert format in ('jpeg', 'png', 'gif', 'webp'), f'Unsupported image format: {format}'
515
- image: ImageBlockTypeDef = {'format': format, 'source': {'bytes': response.content}}
511
+ image: ImageBlockTypeDef = {'format': format, 'source': {'bytes': downloaded_item['data']}}
516
512
  content.append({'image': image})
517
513
 
518
514
  elif item.kind == 'document-url':
519
515
  name = f'Document {next(document_count)}'
520
- data = response.content
521
- content.append({'document': {'name': name, 'format': item.format, 'source': {'bytes': data}}})
516
+ document: DocumentBlockTypeDef = {
517
+ 'name': name,
518
+ 'format': item.format,
519
+ 'source': {'bytes': downloaded_item['data']},
520
+ }
521
+ content.append({'document': document})
522
522
 
523
523
  elif item.kind == 'video-url': # pragma: no branch
524
524
  format = item.media_type.split('/')[1]
525
- assert format in ('mkv', 'mov', 'mp4', 'webm', 'flv', 'mpeg', 'mpg', 'wmv', 'three_gp'), (
526
- f'Unsupported video format: {format}'
527
- )
528
- video: VideoBlockTypeDef = {'format': format, 'source': {'bytes': response.content}}
525
+ assert format in (
526
+ 'mkv',
527
+ 'mov',
528
+ 'mp4',
529
+ 'webm',
530
+ 'flv',
531
+ 'mpeg',
532
+ 'mpg',
533
+ 'wmv',
534
+ 'three_gp',
535
+ ), f'Unsupported video format: {format}'
536
+ video: VideoBlockTypeDef = {'format': format, 'source': {'bytes': downloaded_item['data']}}
529
537
  content.append({'video': video})
530
538
  elif isinstance(item, AudioUrl): # pragma: no cover
531
539
  raise NotImplementedError('Audio is not supported yet.')