openbb-pydantic-ai 0.1.1__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.
- openbb_pydantic_ai/__init__.py +52 -0
- openbb_pydantic_ai/_adapter.py +307 -0
- openbb_pydantic_ai/_config.py +58 -0
- openbb_pydantic_ai/_dependencies.py +73 -0
- openbb_pydantic_ai/_event_builder.py +183 -0
- openbb_pydantic_ai/_event_stream.py +363 -0
- openbb_pydantic_ai/_event_stream_components.py +155 -0
- openbb_pydantic_ai/_event_stream_helpers.py +422 -0
- openbb_pydantic_ai/_exceptions.py +61 -0
- openbb_pydantic_ai/_message_transformer.py +127 -0
- openbb_pydantic_ai/_serializers.py +110 -0
- openbb_pydantic_ai/_toolsets.py +264 -0
- openbb_pydantic_ai/_types.py +39 -0
- openbb_pydantic_ai/_utils.py +132 -0
- openbb_pydantic_ai/_widget_registry.py +145 -0
- openbb_pydantic_ai-0.1.1.dist-info/METADATA +139 -0
- openbb_pydantic_ai-0.1.1.dist-info/RECORD +18 -0
- openbb_pydantic_ai-0.1.1.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: openbb-pydantic-ai
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Pydantic AI adapter for OpenBB SDK. Enables connettion any pydantic-ai agent to the OpenBB Workspace.
|
|
5
|
+
Author: Magnus Samuelsen
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Dist: openbb-ai>=1.7.6
|
|
8
|
+
Requires-Dist: pydantic-ai-slim>=1.12.0
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# OpenBB Pydantic AI Adapter
|
|
13
|
+
|
|
14
|
+
`openbb-pydantic-ai` lets any [Pydantic AI](https://ai.pydantic.dev/) agent
|
|
15
|
+
run behind OpenBB Workspace by translating `QueryRequest` payloads into a Pydantic
|
|
16
|
+
AI run, exposing Workspace widgets as deferred tools, and streaming native
|
|
17
|
+
OpenBB SSE events back to the UI.
|
|
18
|
+
|
|
19
|
+
- **Stateless by design**: each `QueryRequest` already carries the full
|
|
20
|
+
conversation history, widgets, context, and URLs, so the adapter can process
|
|
21
|
+
requests independently.
|
|
22
|
+
- **First-class widget tools**: every widget becomes a deferred Pydantic AI tool;
|
|
23
|
+
when the model calls one, the adapter emits `copilotFunctionCall` events via
|
|
24
|
+
`get_widget_data` and waits for the Workspace to return data before resuming.
|
|
25
|
+
- **Rich event stream**: reasoning steps, “Thinking“ traces, tables, charts, and
|
|
26
|
+
citations are streamed as OpenBB SSE payloads so the Workspace UI can group
|
|
27
|
+
them into dropdowns automatically.
|
|
28
|
+
- **Output helpers included**: structured model outputs (dicts/lists) are
|
|
29
|
+
auto-detected and turned into tables or charts, with chart parameter
|
|
30
|
+
normalization to ensure consistent rendering.
|
|
31
|
+
|
|
32
|
+
To learn more about the underlying SDK types, see the
|
|
33
|
+
[OpenBB Custom Agent SDK repo](https://github.com/OpenBB-finance/openbb-ai)
|
|
34
|
+
and the [Pydantic AI UI adapter docs](https://ai.pydantic.dev/ui/overview/).
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
The adapter is published as a lightweight package, install it wherever you build
|
|
39
|
+
custom agents:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install openbb-pydantic-ai
|
|
43
|
+
# or with uv
|
|
44
|
+
uv add openbb-pydantic-ai
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Quick Start (FastAPI)
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from fastapi import FastAPI, Request
|
|
51
|
+
from pydantic_ai import Agent
|
|
52
|
+
from openbb_pydantic_ai import OpenBBAIAdapter
|
|
53
|
+
|
|
54
|
+
agent = Agent(
|
|
55
|
+
"openai:gpt-4o",
|
|
56
|
+
instructions="Be concise and helpful. Only use widget tools for data lookups.",
|
|
57
|
+
)
|
|
58
|
+
app = FastAPI()
|
|
59
|
+
|
|
60
|
+
@app.post("/query")
|
|
61
|
+
async def query(request: Request):
|
|
62
|
+
return await OpenBBAIAdapter.dispatch_request(request, agent=agent)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### How It Works
|
|
66
|
+
|
|
67
|
+
#### 1. Request Handling
|
|
68
|
+
|
|
69
|
+
- OpenBB Workspace calls the `/query` endpoint with a `QueryRequest` body
|
|
70
|
+
- `OpenBBAIAdapter` validates it and builds the Pydantic AI message stack
|
|
71
|
+
- Workspace context and URLs are injected as system prompts
|
|
72
|
+
|
|
73
|
+
#### 2. Widget Tool Conversion
|
|
74
|
+
|
|
75
|
+
- Widgets in the request become deferred tools
|
|
76
|
+
- Each call emits a `copilotFunctionCall` event (via `get_widget_data`)
|
|
77
|
+
- The adapter pauses until Workspace responds with data
|
|
78
|
+
|
|
79
|
+
#### 3. Event Streaming
|
|
80
|
+
|
|
81
|
+
Pydantic AI events are wrapped into OpenBB SSE events:
|
|
82
|
+
|
|
83
|
+
- **Text chunks** → stream via `copilotMessageChunk`
|
|
84
|
+
- **Reasoning steps** → appear under the "Step-by-step reasoning" dropdown (including Thinking sections)
|
|
85
|
+
- **Tables/charts** → emitted as `copilotMessageArtifact` events with correct chart parameters for consistent rendering
|
|
86
|
+
- **Citations** → fire at the end of the run for every widget tool used
|
|
87
|
+
|
|
88
|
+
### Advanced Usage
|
|
89
|
+
|
|
90
|
+
Need more control? Instantiate the adapter manually:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from openbb_pydantic_ai import OpenBBAIAdapter
|
|
94
|
+
|
|
95
|
+
run_input = OpenBBAIAdapter.build_run_input(body_bytes)
|
|
96
|
+
adapter = OpenBBAIAdapter(agent=agent, run_input=run_input)
|
|
97
|
+
async for event in adapter.run_stream():
|
|
98
|
+
yield event # Already encoded as OpenBB SSE payloads
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
You can also supply `message_history`, `deferred_tool_results`, or `on_complete`
|
|
102
|
+
callbacks—any option supported by `Agent.run_stream_events()` is accepted.
|
|
103
|
+
|
|
104
|
+
## Features
|
|
105
|
+
|
|
106
|
+
### Widget Toolsets
|
|
107
|
+
|
|
108
|
+
- Widgets are grouped by priority (`primary`, `secondary`, `extra`) and exposed
|
|
109
|
+
through dedicated toolsets so you can gate access if needed.
|
|
110
|
+
- Tool names follow `openbb_widget_{origin}_{widget_id}`; the helper
|
|
111
|
+
`build_widget_tool_name` reproduces the exact string for routing.
|
|
112
|
+
|
|
113
|
+
### Deferred Results & Citations
|
|
114
|
+
|
|
115
|
+
- Pending widget responses provided in the request are replayed before the run
|
|
116
|
+
starts, making multi-turn workflows seamless.
|
|
117
|
+
- Every widget call records a citation via `openbb_ai.helpers.cite`, emitted as a
|
|
118
|
+
`copilotCitationCollection` at the end of the run.
|
|
119
|
+
|
|
120
|
+
### Structured Output Detection
|
|
121
|
+
|
|
122
|
+
The adapter provides built-in tools and automatic detection for tables and charts:
|
|
123
|
+
|
|
124
|
+
- **`openbb_create_table`** - Explicitly create table artifacts from structured data
|
|
125
|
+
- **`openbb_create_chart`** - Create chart artifacts (line, bar, scatter, pie, donut) with validation
|
|
126
|
+
- **Auto-detection** - Dict/list outputs shaped like `{"type": "table", "data": [...]}` (or just a list of dicts) automatically become tables
|
|
127
|
+
- **Flexible chart parameters** - Chart outputs tolerate different field spellings (`y_keys`, `yKeys`, etc.) and validate required axes before emitting
|
|
128
|
+
|
|
129
|
+
These tools are always available through the `VisualizationToolset`, allowing agents to explicitly create well-formatted visualizations.
|
|
130
|
+
|
|
131
|
+
## Local Development
|
|
132
|
+
|
|
133
|
+
This repo ships a UV-based workflow:
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
uv sync --dev # install dependencies
|
|
137
|
+
uv run pytest # run the focused test suite
|
|
138
|
+
uv run ty check # static type checking (ty)
|
|
139
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
openbb_pydantic_ai/__init__.py,sha256=8-AFAiME75GvKEk972tutGvm41tY_WIvW5JGqA44T3o,1420
|
|
2
|
+
openbb_pydantic_ai/_adapter.py,sha256=nGvMWibIIqh-EfXJN0aD91NSAOc0CbH01MHJ_8Oex4Y,11063
|
|
3
|
+
openbb_pydantic_ai/_config.py,sha256=3KiS8F1kOBrGXdfBOjVsbcChPcMVJTBv9zlIg2WU1bU,1340
|
|
4
|
+
openbb_pydantic_ai/_dependencies.py,sha256=AtnWtIF-lNjMEvmm9JlYsO10nIZvHt0Lfvzn_QVXzNs,2511
|
|
5
|
+
openbb_pydantic_ai/_event_builder.py,sha256=tIX7RULIP1a2PWbD04if8UUmTwMxYtL6pZymeQnSbek,4973
|
|
6
|
+
openbb_pydantic_ai/_event_stream.py,sha256=1Il3g30tSiOGD5BV6Gc2I0W01QtHEyhBFMJNlmwH46M,11973
|
|
7
|
+
openbb_pydantic_ai/_event_stream_components.py,sha256=kSbAXhnlDVh13AtMeflAXYRcp5rHoALqi0gpxNAmKPU,3794
|
|
8
|
+
openbb_pydantic_ai/_event_stream_helpers.py,sha256=RG-3J-blqAwbh2Wqaf1OKH4dwvMJ7fCnH4vSfjLOwzg,12487
|
|
9
|
+
openbb_pydantic_ai/_exceptions.py,sha256=sLW1vTWTFudng_EFEEt2ngiFJyXUMxPxU4DQuAOpwd0,1949
|
|
10
|
+
openbb_pydantic_ai/_message_transformer.py,sha256=Hgv5mggDQdjHfLqntwNcD9zKsQpVCdS4gtvbZVjcVe8,3980
|
|
11
|
+
openbb_pydantic_ai/_serializers.py,sha256=wWOYIEHECKQF7CNr8CXjtaZtAroOIYCObBYZ2zAumdc,2986
|
|
12
|
+
openbb_pydantic_ai/_toolsets.py,sha256=1VHcFsustybg4XlBrhzVUWGFMbaqOxKe0S3gtX2hcLA,8277
|
|
13
|
+
openbb_pydantic_ai/_types.py,sha256=rl8mHRfozsXmPUEz9q2iu53pYKmBWVQvKyBHM30eg5w,943
|
|
14
|
+
openbb_pydantic_ai/_utils.py,sha256=DkPVuJtBoXFNdPQdmHGfU8hBo1CpWifqyqXrPxHD154,3897
|
|
15
|
+
openbb_pydantic_ai/_widget_registry.py,sha256=daatooFh02RWjEBN1VgQDrYunigeM4zPwSvZRguQ3-U,4326
|
|
16
|
+
openbb_pydantic_ai-0.1.1.dist-info/WHEEL,sha256=DpNsHFUm_gffZe1FgzmqwuqiuPC6Y-uBCzibcJcdupM,78
|
|
17
|
+
openbb_pydantic_ai-0.1.1.dist-info/METADATA,sha256=pa1383gDBunygEGtBpKRws85qEt_Vz0eD2P4k5Lheeo,5259
|
|
18
|
+
openbb_pydantic_ai-0.1.1.dist-info/RECORD,,
|