Skip to content

Commit 92fbff1

Browse files
KludexDouweM
andauthored
Include logfire with pydantic-ai package (#2683)
Co-authored-by: Douwe Maan <[email protected]>
1 parent 9354f08 commit 92fbff1

File tree

8 files changed

+27
-37
lines changed

8 files changed

+27
-37
lines changed

docs/install.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,14 @@ pip/uv-add pydantic-ai
88

99
(Requires Python 3.10+)
1010

11-
This installs the `pydantic_ai` package, core dependencies, and libraries required to use all the models
12-
included in Pydantic AI. If you want to use a specific model, you can install the ["slim"](#slim-install) version of Pydantic AI.
11+
This installs the `pydantic_ai` package, core dependencies, and libraries required to use all the models included in Pydantic AI.
12+
If you want to install only those dependencies required to use a specific model, you can install the ["slim"](#slim-install) version of Pydantic AI.
1313

1414
## Use with Pydantic Logfire
1515

1616
Pydantic AI has an excellent (but completely optional) integration with [Pydantic Logfire](https://pydantic.dev/logfire) to help you view and understand agent runs.
1717

18-
To use Logfire with Pydantic AI, install `pydantic-ai` or `pydantic-ai-slim` with the `logfire` optional group:
19-
20-
```bash
21-
pip/uv-add "pydantic-ai[logfire]"
22-
```
23-
24-
From there, follow the [Logfire setup docs](logfire.md#using-logfire) to configure Logfire.
18+
Logfire comes included with `pydantic-ai` (but not the ["slim" version](#slim-install)), so you can typically start using it immediately by following the [Logfire setup docs](logfire.md#using-logfire).
2519

2620
## Running Examples
2721

docs/logfire.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ A trace is generated for the agent run, and spans are emitted for each model req
3131

3232
## Using Logfire
3333

34-
To use Logfire, you'll need a Logfire [account](https://logfire.pydantic.dev), and the Logfire Python SDK installed:
34+
To use Logfire, you'll need a Logfire [account](https://logfire.pydantic.dev). The Logfire Python SDK is included with `pydantic-ai`:
3535

3636
```bash
37-
pip/uv-add "pydantic-ai[logfire]"
37+
pip/uv-add pydantic-ai
38+
```
39+
40+
Or if you're using the slim package, you can install it with the `logfire` optional group:
41+
42+
```bash
43+
pip/uv-add "pydantic-ai-slim[logfire]"
3844
```
3945

4046
Then authenticate your local environment with Logfire:

pydantic_ai_slim/pydantic_ai/usage.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ class RunUsage(UsageBase):
122122

123123
cache_write_tokens: int = 0
124124
"""Total number of tokens written to the cache."""
125+
125126
cache_read_tokens: int = 0
126127
"""Total number of tokens read from the cache."""
127128

128129
input_audio_tokens: int = 0
129130
"""Total number of audio input tokens."""
131+
130132
cache_audio_read_tokens: int = 0
131133
"""Total number of audio tokens read from the cache."""
132134

pydantic_ai_slim/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ dependencies = [
6666

6767
[tool.hatch.metadata.hooks.uv-dynamic-versioning.optional-dependencies]
6868
# WARNING if you add optional groups, please update docs/install.md
69-
logfire = ["logfire>=3.14.1"]
69+
logfire = ["logfire[httpx]>=3.14.1"]
7070
# Models
7171
openai = ["openai>=1.99.9"]
7272
cohere = ["cohere>=5.16.0; platform_system != 'Emscripten'"]

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ requires-python = ">=3.10"
4646

4747
[tool.hatch.metadata.hooks.uv-dynamic-versioning]
4848
dependencies = [
49-
"pydantic-ai-slim[openai,vertexai,google,groq,anthropic,mistral,cohere,bedrock,huggingface,cli,mcp,evals,ag-ui,retries,temporal]=={{ version }}",
49+
"pydantic-ai-slim[openai,vertexai,google,groq,anthropic,mistral,cohere,bedrock,huggingface,cli,mcp,evals,ag-ui,retries,temporal,logfire]=={{ version }}",
5050
]
5151

5252
[tool.hatch.metadata.hooks.uv-dynamic-versioning.optional-dependencies]
5353
examples = ["pydantic-ai-examples=={{ version }}"]
54-
logfire = ["logfire>=3.14.1"]
5554
a2a = ["fasta2a>=0.4.1"]
5655

5756
[project.urls]

tests/test_examples.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,15 @@
4343

4444
from .conftest import ClientWithHandler, TestEnv, try_import
4545

46-
try:
47-
from pydantic_ai.providers.google import GoogleProvider
48-
except ImportError: # pragma: lax no cover
49-
GoogleProvider = None
50-
51-
52-
try:
53-
import logfire
54-
except ImportError: # pragma: lax no cover
55-
logfire = None
56-
57-
5846
with try_import() as imports_successful:
47+
# We check whether pydantic_ai_examples is importable as a proxy for whether all extras are installed, as some docs examples require them
48+
import pydantic_ai_examples # pyright: ignore[reportUnusedImport] # noqa: F401
49+
5950
from pydantic_evals.reporting import EvaluationReport
6051

6152

6253
pytestmark = [
6354
pytest.mark.skipif(not imports_successful(), reason='extras not installed'),
64-
pytest.mark.skipif(logfire is None or GoogleProvider is None, reason='logfire or google-provider not installed'),
6555
]
6656
code_examples: dict[str, CodeExample] = {}
6757

tests/test_temporal.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@
8181
with workflow.unsafe.imports_passed_through():
8282
# Workaround for a race condition when running `logfire.info` inside an activity with attributes to serialize and pandas importable:
8383
# AttributeError: partially initialized module 'pandas' has no attribute '_pandas_parser_CAPI' (most likely due to a circular import)
84-
import pandas # pyright: ignore[reportUnusedImport] # noqa: F401
84+
try:
85+
import pandas # pyright: ignore[reportUnusedImport] # noqa: F401
86+
except ImportError: # pragma: lax no cover
87+
pass
8588

8689
# https://github.com/temporalio/sdk-python/blob/3244f8bffebee05e0e7efefb1240a75039903dda/tests/test_client.py#L112C1-L113C1
8790
from inline_snapshot import snapshot

uv.lock

Lines changed: 5 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)