Skip to main content

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 QueryRequest already 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 copilotFunctionCall events via get_widget_data and 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

agent = Agent(
    "openai:gpt-4o",
    instructions="Be concise and helpful. Only use widget tools for data lookups.",
)
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 /query endpoint with a QueryRequest body
  • OpenBBAIAdapter validates 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 copilotFunctionCall event (via get_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 copilotMessageArtifact events 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.

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 follow openbb_widget_{origin}_{widget_id}; the helper build_widget_tool_name reproduces the exact string for routing.

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 a copilotCitationCollection at the end of the run.

Structured Output Detection

The adapter provides built-in tools and automatic detection for tables and charts:

  • openbb_create_table - Explicitly create table artifacts from structured data
  • openbb_create_chart - Create chart artifacts (line, bar, scatter, pie, donut) with validation
  • 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

These tools are always available through the VisualizationToolset, allowing agents to explicitly create well-formatted visualizations.

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    # static type checking (ty)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openbb_pydantic_ai-0.1.5.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openbb_pydantic_ai-0.1.5-py3-none-any.whl (27.8 kB view details)

Uploaded Python 3

File details

Details for the file openbb_pydantic_ai-0.1.5.tar.gz.

File metadata

  • Download URL: openbb_pydantic_ai-0.1.5.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"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

Hashes for openbb_pydantic_ai-0.1.5.tar.gz
Algorithm Hash digest
SHA256 3ddc4ce36a8f02d6808d99c7244ccecee48b0175e29d29bc87c601e0b463f124
MD5 765c9ba9adf5fcb5ba5039b8c2927dd6
BLAKE2b-256 1dc4bc8704a74c60cad0034da848283b46d648d78cb2646bedbd1963673a758c

See more details on using hashes here.

File details

Details for the file openbb_pydantic_ai-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: openbb_pydantic_ai-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 27.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"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

Hashes for openbb_pydantic_ai-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f6019e21c5304b7fae0b7db9721666756d0d1dfe060f1da71a0ecb05c7ffb23c
MD5 271240a75af9276799573c71fce8fe4f
BLAKE2b-256 d53a573f5c9c595042d131b765940a72226e6ffdd4fbb0c01be0ac47ab624ce2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page