pydantic-ai-contextfetch 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,102 @@
1
+ """ContextFetch web search and page reading tools for Pydantic AI."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any, Literal
6
+
7
+ from contextfetch import ContextFetch, ContextFetchError
8
+ from pydantic_ai import ModelRetry
9
+ from pydantic_ai.toolsets import FunctionToolset
10
+
11
+ __all__ = ("ContextFetchToolset",)
12
+
13
+ Content = Literal["pruned_context", "text"]
14
+ """`"pruned_context"`: only the parts of each page relevant to the query. `"text"`: the whole page as Markdown."""
15
+
16
+ _RESULT_KEY = {"pruned_context": "prunedContext", "text": "text"}
17
+
18
+
19
+ class ContextFetchToolset(FunctionToolset[Any]):
20
+ """A toolset with two tools that share one ContextFetch client.
21
+
22
+ - `contextfetch_search`: web search; each result carries the page content as Markdown.
23
+ - `contextfetch_read`: one URL as Markdown, optionally only the parts relevant to a question.
24
+
25
+ ```python
26
+ from pydantic_ai import Agent
27
+
28
+ from pydantic_ai_contextfetch import ContextFetchToolset
29
+
30
+ agent = Agent('openai:gpt-5.2', toolsets=[ContextFetchToolset()])
31
+ ```
32
+ """
33
+
34
+ def __init__(
35
+ self,
36
+ api_key: str | None = None,
37
+ *,
38
+ num_results: int = 5,
39
+ content: Content = "pruned_context",
40
+ id: str | None = None,
41
+ ):
42
+ """Creates the toolset.
43
+
44
+ Args:
45
+ api_key: The ContextFetch API key. Defaults to the `CONTEXTFETCH_API_KEY` environment variable;
46
+ without either, requests use the keyless tier.
47
+ num_results: Results per search (1 to 25). Defaults to 5.
48
+ content: Which page content search results carry. Defaults to `"pruned_context"`.
49
+ id: Optional ID for the toolset, used for durable execution environments.
50
+ """
51
+ client = ContextFetch(api_key)
52
+ key = _RESULT_KEY[content]
53
+
54
+ def contextfetch_search(
55
+ query: str,
56
+ include_domains: list[str] | None = None,
57
+ exclude_domains: list[str] | None = None,
58
+ start_published_date: str | None = None,
59
+ ) -> list[dict[str, Any]]:
60
+ """Searches the web and returns the top results with their page content as Markdown.
61
+
62
+ Args:
63
+ query: The web search query.
64
+ include_domains: Only return results from these domains.
65
+ exclude_domains: Never return results from these domains.
66
+ start_published_date: Only return pages published on or after this date (YYYY-MM-DD).
67
+ """
68
+ response = client.search(
69
+ query,
70
+ num_results=num_results,
71
+ include_domains=include_domains,
72
+ exclude_domains=exclude_domains,
73
+ start_published_date=start_published_date,
74
+ text=content == "text",
75
+ pruned_context=content == "pruned_context",
76
+ )
77
+ return [
78
+ {
79
+ "title": r.get("title"),
80
+ "url": r.get("url"),
81
+ "published_date": r.get("publishedDate"),
82
+ "content": r.get(key) or "",
83
+ }
84
+ for r in response.get("results", [])
85
+ ]
86
+
87
+ def contextfetch_read(url: str, query: str | None = None) -> str:
88
+ """Reads a web page and returns it as Markdown.
89
+
90
+ Args:
91
+ url: The URL of the page.
92
+ query: If set, only the parts of the page relevant to this question are returned.
93
+ """
94
+ try:
95
+ return client.read(url, query=query).markdown
96
+ except ContextFetchError as e:
97
+ # A bad or unreachable URL is something the model can fix; auth, credit and server errors are not.
98
+ if e.status in (400, 404, 422):
99
+ raise ModelRetry(str(e)) from e
100
+ raise
101
+
102
+ super().__init__([contextfetch_search, contextfetch_read], id=id)
@@ -0,0 +1,68 @@
1
+ Metadata-Version: 2.5
2
+ Name: pydantic-ai-contextfetch
3
+ Version: 0.1.0
4
+ Summary: ContextFetch web search and page reading tools for Pydantic AI
5
+ Project-URL: Homepage, https://contextfetch.dev
6
+ Project-URL: Documentation, https://contextfetch.dev/docs/reference
7
+ Project-URL: Repository, https://github.com/lachy3433/pydantic-ai-contextfetch
8
+ Author: ContextFetch
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Classifier: Framework :: Pydantic
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
15
+ Requires-Python: >=3.10
16
+ Requires-Dist: contextfetch<1.0.0,>=0.1.0
17
+ Requires-Dist: pydantic-ai-slim>=1.0.0
18
+ Description-Content-Type: text/markdown
19
+
20
+ # pydantic-ai-contextfetch
21
+
22
+ [ContextFetch](https://contextfetch.dev) web search and page reading tools for [Pydantic AI](https://ai.pydantic.dev), as a toolset.
23
+
24
+ ```bash
25
+ pip install pydantic-ai-contextfetch
26
+ export CONTEXTFETCH_API_KEY="your-api-key" # optional: without a key, requests use the keyless tier
27
+ ```
28
+
29
+ ```python
30
+ from pydantic_ai import Agent
31
+
32
+ from pydantic_ai_contextfetch import ContextFetchToolset
33
+
34
+ agent = Agent('openai:gpt-5.2', toolsets=[ContextFetchToolset()])
35
+ result = agent.run_sync('What changed in the latest Python release?')
36
+ print(result.output)
37
+ ```
38
+
39
+ ## Tools
40
+
41
+ - `contextfetch_search(query, include_domains?, exclude_domains?, start_published_date?)`: web search. Returns a list
42
+ of `{title, url, published_date, content}`, where `content` is the page as Markdown: by default only the parts
43
+ relevant to the query.
44
+ - `contextfetch_read(url, query?)`: one page as Markdown. With `query`, only the parts relevant to it. A bad or
45
+ unreachable URL raises `ModelRetry`, so the model can correct it.
46
+
47
+ ## Options
48
+
49
+ ```python
50
+ ContextFetchToolset(
51
+ api_key=None, # defaults to CONTEXTFETCH_API_KEY
52
+ num_results=5, # results per search, 1 to 25
53
+ content='pruned_context', # or 'text' for whole pages
54
+ id=None, # toolset id for durable execution
55
+ )
56
+ ```
57
+
58
+ To expose only one tool, use Pydantic AI's toolset filtering:
59
+ `ContextFetchToolset().filtered(lambda ctx, tool: tool.name == 'contextfetch_search')`.
60
+
61
+ ## Tests
62
+
63
+ ```bash
64
+ uv run --group test pytest tests # unit tests, no network
65
+ CONTEXTFETCH_API_KEY=... uv run --group test pytest tests/test_live.py # live, 2 credits
66
+ ```
67
+
68
+ MIT licensed.
@@ -0,0 +1,5 @@
1
+ pydantic_ai_contextfetch/__init__.py,sha256=Ju5JdGMGFWGuQ6aKrB0bYgn4Y5VSkXNB5PUGNwvk58Q,3916
2
+ pydantic_ai_contextfetch-0.1.0.dist-info/METADATA,sha256=yonz8BHygNe3Lf0-dA91zDMEYt87-wjksjm8-544yKM,2438
3
+ pydantic_ai_contextfetch-0.1.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
4
+ pydantic_ai_contextfetch-0.1.0.dist-info/licenses/LICENSE,sha256=RqQqv3BiTnGfaC6tdJ9oSSRKa0fzw4e7wxJwAR9Bab4,1073
5
+ pydantic_ai_contextfetch-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.32.4
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2026 ContextFetch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.