Skip to content

Commit c1917f5

Browse files
committed
Add public trace example
1 parent 8d111fe commit c1917f5

File tree

2 files changed

+48
-41
lines changed

2 files changed

+48
-41
lines changed

docs/index.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,10 @@ async def add_customer_name(ctx: RunContext[SupportDependencies]) -> str:
134134

135135

136136
@support_agent.tool # (6)!
137-
async def customer_balance(
138-
ctx: RunContext[SupportDependencies], include_pending: bool
139-
) -> float:
137+
async def customer_balance(ctx: RunContext[SupportDependencies]) -> float:
140138
"""Returns the customer's current account balance.""" # (7)!
141139
return await ctx.deps.db.customer_balance(
142140
id=ctx.deps.customer_id,
143-
include_pending=include_pending,
144141
)
145142

146143

@@ -196,7 +193,7 @@ import logfire
196193

197194
logfire.configure() # (1)!
198195
logfire.instrument_pydantic_ai() # (2)!
199-
logfire.instrument_asyncpg() # (3)!
196+
logfire.instrument_sqlite3() # (3)!
200197

201198
...
202199

@@ -213,11 +210,14 @@ support_agent = Agent(
213210

214211
1. Configure the Logfire SDK, this will fail if project is not set up.
215212
2. This will instrument all Pydantic AI agents used from here on out. If you want to instrument only a specific agent, you can pass the [`instrument=True` keyword argument][pydantic_ai.Agent.__init__] to the agent.
216-
3. In our demo, `DatabaseConn` uses [`asyncpg`]() to connect to a PostgreSQL database, so [`logfire.instrument_asyncpg()`](https://magicstack.github.io/asyncpg/current/) is used to log the database queries.
213+
3. In our demo, `DatabaseConn` uses [`sqlite3`][] to connect to a PostgreSQL database, so [`logfire.instrument_sqlite3()`](https://logfire.pydantic.dev/docs/integrations/databases/sqlite3/)
214+
is used to log the database queries.
217215

218216
That's enough to get the following view of your agent in action:
219217

220-
{{ video('9078b98c4f75d01f912a0368bbbdb97a', 25, 55) }}
218+
/// public-trace | https://logfire-eu.pydantic.dev/public-trace/a2957caa-b7b7-4883-a529-777742649004?spanId=31aade41ab896144
219+
title: 'Logfire instrumentation for the bank agent'
220+
///
221221

222222
See [Monitoring and Performance](logfire.md) to learn more.
223223

examples/pydantic_ai_examples/bank_support.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,37 @@
55
uv run -m pydantic_ai_examples.bank_support
66
"""
77

8+
import sqlite3
89
from dataclasses import dataclass
910

1011
from pydantic import BaseModel
1112

1213
from pydantic_ai import Agent, RunContext
1314

1415

16+
@dataclass
1517
class DatabaseConn:
16-
"""This is a fake database for example purposes.
17-
18-
In reality, you'd be connecting to an external database
19-
(e.g. PostgreSQL) to get information about customers.
20-
"""
21-
22-
@classmethod
23-
async def customer_name(cls, *, id: int) -> str | None:
24-
if id == 123:
25-
return 'John'
26-
27-
@classmethod
28-
async def customer_balance(cls, *, id: int, include_pending: bool) -> float:
29-
if id == 123:
30-
if include_pending:
31-
return 123.45
32-
else:
33-
return 100.00
18+
"""A wrapper over the SQLite connection."""
19+
20+
sqlite_conn: sqlite3.Connection
21+
22+
async def customer_name(self, *, id: int) -> str | None:
23+
res = cur.execute('SELECT name FROM customers WHERE id=?', (id,))
24+
row = res.fetchone()
25+
if row:
26+
return row[0]
27+
return None
28+
29+
async def customer_balance(self, *, id: int) -> float:
30+
res = cur.execute('SELECT balance FROM customers WHERE id=?', (id,))
31+
row = res.fetchone()
32+
if row:
33+
return row[0]
3434
else:
3535
raise ValueError('Customer not found')
3636

3737

38+
3839
@dataclass
3940
class SupportDependencies:
4041
customer_id: int
@@ -69,27 +70,33 @@ async def add_customer_name(ctx: RunContext[SupportDependencies]) -> str:
6970

7071

7172
@support_agent.tool
72-
async def customer_balance(
73-
ctx: RunContext[SupportDependencies], include_pending: bool
74-
) -> str:
73+
async def customer_balance(ctx: RunContext[SupportDependencies]) -> str:
7574
"""Returns the customer's current account balance."""
7675
balance = await ctx.deps.db.customer_balance(
7776
id=ctx.deps.customer_id,
78-
include_pending=include_pending,
7977
)
8078
return f'${balance:.2f}'
8179

8280

8381
if __name__ == '__main__':
84-
deps = SupportDependencies(customer_id=123, db=DatabaseConn())
85-
result = support_agent.run_sync('What is my balance?', deps=deps)
86-
print(result.output)
87-
"""
88-
support_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1
89-
"""
90-
91-
result = support_agent.run_sync('I just lost my card!', deps=deps)
92-
print(result.output)
93-
"""
94-
support_advice="I'm sorry to hear that, John. We are temporarily blocking your card to prevent unauthorized transactions." block_card=True risk=8
95-
"""
82+
with sqlite3.connect(':memory:') as con:
83+
cur = con.cursor()
84+
cur.execute('CREATE TABLE customers(id, name, balance)')
85+
cur.execute("""
86+
INSERT INTO customers VALUES
87+
(123, 'John', 123.45)
88+
""")
89+
con.commit()
90+
91+
deps = SupportDependencies(customer_id=123, db=DatabaseConn(sqlite_conn=con))
92+
result = support_agent.run_sync('What is my balance?', deps=deps)
93+
print(result.output)
94+
"""
95+
support_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1
96+
"""
97+
98+
result = support_agent.run_sync('I just lost my card!', deps=deps)
99+
print(result.output)
100+
"""
101+
support_advice="I'm sorry to hear that, John. We are temporarily blocking your card to prevent unauthorized transactions." block_card=True risk=8
102+
"""

0 commit comments

Comments
 (0)