Skip to content

feat(governance): Pydantic AI adapter#359

Open
aditik0303 wants to merge 5 commits into
mainfrom
feat/governance-pydantic-ai
Open

feat(governance): Pydantic AI adapter#359
aditik0303 wants to merge 5 commits into
mainfrom
feat/governance-pydantic-ai

Conversation

@aditik0303

@aditik0303 aditik0303 commented Jun 24, 2026

Copy link
Copy Markdown

Summary

Adds a Pydantic AI governance adapter to uipath-pydantic-ai. It lets UiPath governance evaluate what a Pydantic AI Agent does at the model and tool level, and block disallowed actions, without the agent author writing governance code. This package contains only the Pydantic-AI-specific bridge.

What it does

  • Detects a pydantic_ai.Agent (can_handle claims only real Agent instances) and governs it by wrapping agent.model with a GovernanceModel (a pydantic_ai WrapperModel). Both the non-streaming request and the streaming request_stream paths are bracketed.

  • Everything flows through the model, so governance hooks are derived from the request/response messages rather than from per-event callbacks:

    Pydantic AI signal Governance hook
    latest request message text (user prompt or tool result) BEFORE_MODEL
    model response text AFTER_MODEL
    each ToolCallPart the model emits TOOL_CALL
    each ToolReturnPart in the request AFTER_TOOL
  • Only the latest request message is scanned each turn, so a prompt or tool result is not re-evaluated every time the full history is re-sent for context.

  • Enforces by letting a GovernanceBlockException (raised on a DENY decision) propagate, stopping the model call or tool. Any other error inside a governance hook is logged and swallowed, so a governance failure cannot break an otherwise-healthy agent run.

  • Discovered through the uipath.governance.adapters entry point, so no explicit import is needed to register it.

What it does not do

  • Does not fire agent-level boundaries (BEFORE_AGENT / AFTER_AGENT); those are owned by the governance host.
  • Does not depend on platform, auth, transport, or runtime internals — only on the shared governance contracts in uipath-core.

aditik0303 and others added 2 commits June 22, 2026 23:09
Wraps agent.model with a WrapperModel deriving all four hooks from message parts (UserPromptPart -> BEFORE_MODEL, TextPart -> AFTER_MODEL, ToolCallPart -> TOOL_CALL, ToolReturnPart -> AFTER_TOOL); covers request and request_stream.

Self-registers via the uipath.governance.adapters entry point; unit-tested and verified firing through the framework's real execution path. BEFORE/AFTER_AGENT remain owned by the uipath-runtime wrapper.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…, framework-only can_handle)

Mirror radu's LangChain-adapter review across the Pydantic AI adapter:
- __init__: drop the import-time registration side-effect; registration only via the uipath.governance.adapters entry point.
- can_handle: claim only a real pydantic_ai.Agent; remove the duck-typed (model/run/iter) fallback.
- docstring: 'governance host' instead of uipath-runtime internals.
- tests: a duck-typed look-alike is now rejected.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 24, 2026 12:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a governance adapter for the uipath-pydantic-ai integration, enabling UiPath governance evaluators to observe and enforce policy around Pydantic AI model calls and tool usage via model-wrapping, with registration done through entry-point discovery.

Changes:

  • Introduces PydanticAIAdapter, GovernanceModel, and GovernanceCallbacks to bracket model requests/responses and tool events with governance hooks.
  • Adds an entry point (uipath.governance.adapters) to register the adapter without import-time mutation of the global registry.
  • Adds a focused unit test suite that uses real pydantic_ai types and rejects duck-typed look-alikes.

Reviewed changes

Copilot reviewed 4 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/uipath-pydantic-ai/src/uipath_pydantic_ai/governance/adapter.py Implements the Pydantic AI governance adapter and model wrapper that fires BEFORE_MODEL / AFTER_MODEL / TOOL_CALL / AFTER_TOOL.
packages/uipath-pydantic-ai/src/uipath_pydantic_ai/governance/init.py Provides an idempotent register_governance_adapter() used by entry-point discovery (no import-time registration).
packages/uipath-pydantic-ai/tests/governance/test_adapter.py Adds unit tests for can-handle behavior, attach/detach model wrapping, hook firing, and exception propagation/swallowing rules.
packages/uipath-pydantic-ai/tests/governance/init.py Adds the governance tests package marker.
packages/uipath-pydantic-ai/pyproject.toml Adds uipath-core dependency and a governance adapter entry point.
packages/uipath-pydantic-ai/uv.lock Locks the added uipath-core dependency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/uipath-pydantic-ai/src/uipath_pydantic_ai/governance/adapter.py Outdated
aditik0303 and others added 3 commits June 24, 2026 18:26
…g path

Address Copilot review on the Pydantic AI adapter:
- request_stream's after-stream check caught all exceptions, swallowing GovernanceBlockException — a DENY during streaming did not abort the run, unlike the non-streaming request() path. Re-raise the block exception; keep swallowing other governance errors. Add a streaming block-propagation test.
- Module docstring: registers via the uipath.governance.adapters entry point, not at import time.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
These files were swept into the branch by a broad add; they are unrelated to the governance adapter. Reverting/removing them so the PR contains only governance changes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
An earlier cleanup commit compared against a stale local main and wrongly removed SETUP.MD and reverted the LlamaIndex docs change. Both files come from main (PRs #352/#356), not this branch. Restore them to the main version so this PR is governance-only with no spurious deletions.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
80.0% Coverage on New Code (required ≥ 90%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants