Pydantic AI adapter for OpenBB SDK. Enables connettion any pydantic-ai agent to the OpenBB Workspace.
Project description
OpenBB Pydantic AI Adapter
openbb-pydantic-ai lets any Pydantic AI agent
run behind OpenBB Workspace by translating QueryRequest payloads into a Pydantic
AI run, exposing Workspace widgets as deferred tools, and streaming native
OpenBB SSE events back to the UI.
- Stateless by design: each
QueryRequestalready carries the full conversation history, widgets, context, and URLs, so the adapter can process requests independently. - First-class widget tools: every widget becomes a deferred Pydantic AI tool;
when the model calls one, the adapter emits
copilotFunctionCallevents viaget_widget_dataand waits for the Workspace to return data before resuming. - Rich event stream: reasoning steps, “Thinking“ traces, tables, charts, and citations are streamed as OpenBB SSE payloads so the Workspace UI can group them into dropdowns automatically.
- Output helpers included: structured model outputs (dicts/lists) are auto-detected and turned into tables or charts, with chart parameter normalization to ensure consistent rendering.
To learn more about the underlying SDK types, see the OpenBB Custom Agent SDK repo and the Pydantic AI UI adapter docs.
Installation
The adapter is published as a lightweight package, install it wherever you build custom agents:
pip install openbb-pydantic-ai
# or with uv
uv add openbb-pydantic-ai
Quick Start (FastAPI)
from fastapi import FastAPI, Request
from pydantic_ai import Agent
from openbb_pydantic_ai import OpenBBAIAdapter, OpenBBDeps
agent = Agent(
"openai:gpt-5",
instructions="Be concise and helpful. Only use widget tools for data lookups.",
deps_type=OpenBBDeps,
)
app = FastAPI()
@app.post("/query")
async def query(request: Request):
return await OpenBBAIAdapter.dispatch_request(request, agent=agent)
How It Works
1. Request Handling
- OpenBB Workspace calls the
/queryendpoint with aQueryRequestbody OpenBBAIAdaptervalidates it and builds the Pydantic AI message stack- Workspace context and URLs are injected as system prompts
2. Widget Tool Conversion
- Widgets in the request become deferred tools
- Each call emits a
copilotFunctionCallevent (viaget_widget_data) - The adapter pauses until Workspace responds with data
3. Event Streaming
Pydantic AI events are wrapped into OpenBB SSE events:
- Text chunks → stream via
copilotMessageChunk - Reasoning steps → appear under the "Step-by-step reasoning" dropdown (including Thinking sections)
- Tables/charts → emitted as
copilotMessageArtifactevents with correct chart parameters for consistent rendering - Citations → fire at the end of the run for every widget tool used
Advanced Usage
Need more control? Instantiate the adapter manually:
from openbb_pydantic_ai import OpenBBAIAdapter
run_input = OpenBBAIAdapter.build_run_input(body_bytes)
adapter = OpenBBAIAdapter(agent=agent, run_input=run_input)
async for event in adapter.run_stream():
yield event # Already encoded as OpenBB SSE payloads
You can also supply message_history, deferred_tool_results, or on_complete
callbacks—any option supported by Agent.run_stream_events() is accepted.
Runtime deps & prompts
OpenBBDepsbundles widgets (grouped by priority), context rows, relevant URLs, workspace state, timezone, and a serializedstatedict you can pass to toolsets or output validators.- The adapter merges dashboard context and current widget parameter values into the runtime instructions automatically; append your own instructions without re-supplying that context.
Features
Widget Toolsets
- Widgets are grouped by priority (
primary,secondary,extra) and exposed through dedicated toolsets so you can gate access if needed. - Tool names start with
openbb_widget_plus the widget identifier; any redundantopenbb_prefix from the origin is trimmed so names stay concise (e.g.,openbb_widget_sandbox_financial_statements). Usebuild_widget_tool_nameto reproduce the routing string exactly.
MCP Tools
- Any tools listed in
QueryRequest.toolsare exposed as a external MCP toolset, so the model can call the same names the Workspace UI presents. - Deferred
execute_agent_toolresults replay on the next request just like widget results, keeping multi-turn streaming consistent.
Deferred Results & Citations
- Pending widget responses provided in the request are replayed before the run starts, making multi-turn workflows seamless.
- Every widget call records a citation via
openbb_ai.helpers.cite, emitted as acopilotCitationCollectionat the end of the run.
Structured Output Detection
The adapter provides built-in helpers and automatic detection for charts and tables:
- Markdown tables - Stream tabular data as Markdown; Workspace renders them as tables and lets users promote them to widgets.
openbb_create_chart- Create chart artifacts (line, bar, scatter, pie, donut) with validation. Insert{{place_chart_here}}in the response where the chart should appear; the adapter swaps that placeholder for the rendered artifact while streaming.- Auto-detection - Dict/list outputs shaped like
{"type": "table", "data": [...]}(or just a list of dicts) automatically become tables. - Flexible chart parameters - Chart outputs tolerate different field spellings (
y_keys,yKeys, etc.) and validate required axes before emitting. openbb_create_table- Explicitly create a table artifact from structured data when you want predictable column ordering and metadata.
Local Development
This repo ships a UV-based workflow:
uv sync --dev # install dependencies
uv run pytest # run the focused test suite
uv run ty check # type checking (ty)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openbb_pydantic_ai-0.1.6.tar.gz.
File metadata
- Download URL: openbb_pydantic_ai-0.1.6.tar.gz
- Upload date:
- Size: 26.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24f3047b88e6c9824f28065a2b11ef603711b7080b60215446bfdabbd4f5077e
|
|
| MD5 |
ee6095e9e5632972821e4ceb47f6e77e
|
|
| BLAKE2b-256 |
aeb9166cfbac687eecef40636f330ef55f31851679ce1927950000f3388a77f5
|
File details
Details for the file openbb_pydantic_ai-0.1.6-py3-none-any.whl.
File metadata
- Download URL: openbb_pydantic_ai-0.1.6-py3-none-any.whl
- Upload date:
- Size: 33.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a591219d15affe88dc6090ab2b0bd517173af13511db8cee4e8af38e12ca3e85
|
|
| MD5 |
d4128b96028591411accfde915d90aac
|
|
| BLAKE2b-256 |
bbf60bbc8ba58af93595811cce8f526c00c4cc38cf580aa28a8504baa61b51aa
|