Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ env*/
/TODO.md
/postgres-data/
.DS_Store
/pydantic_ai_examples/.chat_app_messages.sqlite
examples/pydantic_ai_examples/.chat_app_messages.sqlite
.cache/
.vscode/
4 changes: 2 additions & 2 deletions docs/.hooks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ def sub_run(m: re.Match[str]) -> str:
```"""


EXAMPLES_DIR = Path(__file__).parent.parent.parent / 'pydantic_ai_examples'
EXAMPLES_DIR = Path(__file__).parent.parent.parent / 'examples'


def render_examples(markdown: str) -> str:
return re.sub(r'^#! *pydantic_ai_examples/(.+)', sub_example, markdown, flags=re.M)
return re.sub(r'^#! *examples/(.+)', sub_example, markdown, flags=re.M)


def sub_example(m: re.Match[str]) -> str:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/bank-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ python/uv-run -m pydantic_ai_examples.bank_support
## Example Code

```python {title="bank_support.py"}
#! pydantic_ai_examples/bank_support.py
#! examples/pydantic_ai_examples/bank_support.py
```
6 changes: 3 additions & 3 deletions docs/examples/chat-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ TODO screenshot.
Python code that runs the chat app:

```python {title="chat_app.py"}
#! pydantic_ai_examples/chat_app.py
#! examples/pydantic_ai_examples/chat_app.py
```

Simple HTML page to render the app:

```html {title="chat_app.html"}
#! pydantic_ai_examples/chat_app.html
#! examples/pydantic_ai_examples/chat_app.html
```

TypeScript to handle rendering the messages, to keep this simple (and at the risk of offending frontend developers) the typescript code is passed to the browser as plain text and transpiled in the browser.

```ts {title="chat_app.ts"}
#! pydantic_ai_examples/chat_app.ts
#! examples/pydantic_ai_examples/chat_app.ts
```
2 changes: 1 addition & 1 deletion docs/examples/pydantic-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ PYDANTIC_AI_MODEL=gemini-1.5-pro python/uv-run -m pydantic_ai_examples.pydantic_
## Example Code

```python {title="pydantic_model.py"}
#! pydantic_ai_examples/pydantic_model.py
#! examples/pydantic_ai_examples/pydantic_model.py
```
2 changes: 1 addition & 1 deletion docs/examples/rag.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ python/uv-run -m pydantic_ai_examples.rag search "How do I configure logfire to
## Example Code

```python {title="rag.py"}
#! pydantic_ai_examples/rag.py
#! examples/pydantic_ai_examples/rag.py
```
2 changes: 1 addition & 1 deletion docs/examples/sql-gen.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ This model uses `gemini-1.5-flash` by default since Gemini is good at single sho
## Example Code

```python {title="sql_gen.py"}
#! pydantic_ai_examples/sql_gen.py
#! examples/pydantic_ai_examples/sql_gen.py
```
2 changes: 1 addition & 1 deletion docs/examples/stream-markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ python/uv-run -m pydantic_ai_examples.stream_markdown
## Example Code

```python
#! pydantic_ai_examples/stream_markdown.py
#! examples/pydantic_ai_examples/stream_markdown.py
```
2 changes: 1 addition & 1 deletion docs/examples/stream-whales.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ Should give an output like this:
## Example Code

```python {title="stream_whales.py"}
#! pydantic_ai_examples/stream_whales.py
#! examples/pydantic_ai_examples/stream_whales.py
```
2 changes: 1 addition & 1 deletion docs/examples/weather-agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ python/uv-run -m pydantic_ai_examples.weather_agent
## Example Code

```python {title="pydantic_ai_examples/weather_agent.py"}
#! pydantic_ai_examples/weather_agent.py
#! examples/pydantic_ai_examples/weather_agent.py
```
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ async def customer_balance(
return f'${balance:.2f}'


deps = SupportDependencies(customer_id=123, db=DatabaseConn())
result = support_agent.run_sync('What is my balance?', deps=deps)
print(result.data)
"""
support_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1
"""
if __name__ == '__main__':
deps = SupportDependencies(customer_id=123, db=DatabaseConn())
result = support_agent.run_sync('What is my balance?', deps=deps)
print(result.data)
"""
support_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1
"""

result = support_agent.run_sync('I just lost my card!', deps=deps)
print(result.data)
"""
support_advice="I'm sorry to hear that, John. We are temporarily blocking your card to prevent unauthorized transactions." block_card=True risk=8
"""
result = support_agent.run_sync('I just lost my card!', deps=deps)
print(result.data)
"""
support_advice="I'm sorry to hear that, John. We are temporarily blocking your card to prevent unauthorized transactions." block_card=True risk=8
"""
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
uv run -m pydantic_ai_examples.whales
"""

from typing import Annotated, NotRequired, TypedDict
from typing import Annotated

import devtools
import logfire
from pydantic import Field, ValidationError
from rich.console import Console
from rich.live import Live
from rich.table import Table
from typing_extensions import NotRequired, TypedDict

from pydantic_ai import Agent

Expand All @@ -41,11 +41,6 @@ class Whale(TypedDict):
agent = Agent('openai:gpt-4', result_type=list[Whale])


def check_validation_error(e: ValidationError) -> bool:
devtools.debug(e.errors())
return False


async def main():
console = Console()
with Live('\n' * 36, console=console) as live:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ dependencies = [
"uvicorn>=0.32.0",
]

[tool.hatch.build]
include = ["*.py", "*.html", "*.ts"]

[tool.hatch.build.targets.wheel.sources]
"" = "pydantic_ai_examples"
[tool.hatch.build.targets.wheel]
packages = ["pydantic_ai_examples"]

[tool.uv.sources]
pydantic-ai-slim = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ markdown_extensions:

watch:
- pydantic_ai_slim
- pydantic_ai_examples
- examples

plugins:
- search
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pydantic-ai-slim = { workspace = true }
pydantic-ai-examples = { workspace = true }

[tool.uv.workspace]
members = ["pydantic_ai_slim", "pydantic_ai_examples"]
members = ["pydantic_ai_slim", "examples"]

[dependency-groups]
# dev dependencies are defined in `pydantic-ai-slim/pyproject.toml` to allow for minimal testing
Expand Down Expand Up @@ -82,7 +82,7 @@ line-length = 120
target-version = "py39"
include = [
"pydantic_ai_slim/**/*.py",
"pydantic_ai_examples/**/*.py",
"examples/**/*.py",
"tests/**/*.py",
"docs/**/*.py",
]
Expand Down Expand Up @@ -118,14 +118,14 @@ quote-style = "single"
[tool.ruff.lint.per-file-ignores]
"tests/**/*.py" = ["D"]
"docs/**/*.py" = ["D"]
"pydantic_ai_examples/**/*.py" = ["D101", "D103"]
"examples/**/*.py" = ["D101", "D103"]

[tool.pyright]
typeCheckingMode = "strict"
reportMissingTypeStubs = false
reportUnnecessaryIsInstance = false
reportUnnecessaryTypeIgnoreComment = true
include = ["pydantic_ai_slim", "tests", "pydantic_ai_examples"]
include = ["pydantic_ai_slim", "tests", "examples"]
venvPath = ".venv"
# see https://github.com/microsoft/pyright/issues/7771 - we don't want to error on decorated functions in tests
# which are not otherwise used
Expand Down
19 changes: 15 additions & 4 deletions tests/import_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@
"""

import os
import sys
from pathlib import Path

os.environ.update(OPENAI_API_KEY='fake-key', GEMINI_API_KEY='fake-key')
if sys.version_info < (3, 11):
print('Skipping import_examples.py because it requires Python 3.11+')
else:
os.environ.update(OPENAI_API_KEY='fake-key', GEMINI_API_KEY='fake-key', GROQ_API_KEY='fake-key')

examples_dir = Path(__file__).parent.parent / 'examples'
for example in examples_dir.glob('*.py'):
__import__(f'examples.{example.stem}')
examples_dir = Path(__file__).parent.parent / 'examples' / 'pydantic_ai_examples'
assert examples_dir.is_dir(), f'No examples directory found at {examples_dir}'
count = 0
for example in examples_dir.glob('*.py'):
print(f'Importing {example.stem}...')
__import__(f'pydantic_ai_examples.{example.stem}')
count += 1

print(f'Imported {count} examples')
assert count > 5, 'No examples found'
2 changes: 1 addition & 1 deletion uprev.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def replace_deps_version(text: str) -> tuple[str, int]:

root_pp_text, count_root = replace_deps_version(root_pp_text)

examples_pp = ROOT_DIR / 'pydantic_ai_examples' / 'pyproject.toml'
examples_pp = ROOT_DIR / 'examples' / 'pyproject.toml'
examples_pp_text = examples_pp.read_text()
examples_pp_text, count_ex = replace_deps_version(examples_pp_text)

Expand Down
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading