Skip to content

fix: AgentRunResult.usage API compatibility on pydantic-ai 2.x - #1091

Open
vitali87 wants to merge 3 commits into
mainfrom
fix/agent-run-result-usage-property
Open

fix: AgentRunResult.usage API compatibility on pydantic-ai 2.x#1091
vitali87 wants to merge 3 commits into
mainfrom
fix/agent-run-result-usage-property

Conversation

@vitali87

@vitali87 vitali87 commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Based on #964 by @subhamruhela13.

Summary

A fresh pip install code-graph-rag resolves pydantic-ai 2.x, where AgentRunResult.usage is a property; the interactive chat then crashes on its first response with TypeError: 'RunUsage' object is not callable at codebase_rag/main.py:655. The dev lock held pydantic-ai 1.102.0 (where usage is still a method), which is why the suite stayed green while every fresh install was broken.

Changes

  • response.usage() -> response.usage (cherry-picked from fix: AgentRunResult.usage API compatibility #964, authorship preserved)
  • pydantic-ai>=2.0.0 floor (the property landed in 2.0.0; verified GoogleCloudProvider, AgentRetries, and the deferred-tools API all exist there) and uv.lock upgraded to 2.24.0
  • Vertex model creation moved from GoogleProvider(project=..., location=..., credentials=...) (removed in 2.x) to GoogleCloudProvider, the same construction path 1.102.0 delegated to behind a deprecation warning
  • Dropped the griffe>=1.0,<2 workaround: pydantic-ai 2.x declares griffelib, which installs the same top-level griffe/ package, so the old pin made both packages write one directory
  • Regenerated the docs dependency section

Review fixes applied

  • CodeRabbit on fix: AgentRunResult.usage API compatibility #964: the three usage.return_value mocks in test_model_switching.py now assign RunUsage property-style, so the loop consumes realistic token data
  • Added a RED->GREEN test pinning the Vertex branch to GoogleCloudProvider(project/location/credentials); without it the suite passes even if the Vertex path is reverted to the 1.x constructor

Test plan

  • uv run pytest -m "not integration and not e2e and not slow": 6459 passed; test_vector_store.py + test_unixcoder_unit.py pass with the semantic/milvus extras installed
  • Real gesture: cgr start chat against local Ollama completes with real RunUsage token accounting instead of the TypeError
  • ty check clean with the repo's pre-commit scoping

Summary by CodeRabbit

  • New Features

    • Improved Google Vertex AI integration with clearer project, region, and credential configuration.
    • Updated model usage handling for more reliable agent responses and usage reporting.
  • Bug Fixes

    • Fixed compatibility with the latest supported AI framework version.
    • Corrected response usage processing to prevent runtime errors.
  • Documentation

    • Simplified installation guidance by removing an obsolete setup requirement.

subhamruhela13 and others added 3 commits August 5, 2026 13:13
The latest AgentRunResult exposes 'usage' as a property instead of a callable method. 
Replace: response.usage() with: response.usage 
This fixes: TypeError: 'RunUsage' object is not callable when running 'cgr start' with newer versions of pydantic-ai/OpenAI agent libraries.
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: cb5bc261-a6c4-4554-85c7-6d76feef9de3

📥 Commits

Reviewing files that changed from the base of the PR and between 43451bb and 3536615.

⛔ Files ignored due to path filters (1)
  • uv.lock is excluded by !**/*.lock
📒 Files selected for processing (6)
  • codebase_rag/main.py
  • codebase_rag/providers/base.py
  • codebase_rag/tests/test_model_switching.py
  • codebase_rag/tests/test_provider_classes.py
  • docs/getting-started/installation.md
  • pyproject.toml
💤 Files with no reviewable changes (1)
  • docs/getting-started/installation.md

📝 Walkthrough

Walkthrough

The PR updates pydantic-ai compatibility, changes response usage access to a property, and switches Google Vertex model creation to GoogleCloudProvider. Tests cover response usage mocks and Vertex provider construction.

Changes

Provider and usage compatibility

Layer / File(s) Summary
Usage API upgrade
pyproject.toml, codebase_rag/main.py, codebase_rag/tests/test_model_switching.py, docs/getting-started/installation.md
The minimum pydantic-ai version is 2.0.0. Response usage is read as a property, and test responses provide RunUsage directly. The installation guide removes griffe from the key dependencies list.
Vertex provider construction
codebase_rag/providers/base.py, codebase_rag/tests/test_provider_classes.py
Google Vertex model creation uses GoogleCloudProvider with the configured project, region, and credentials. Tests verify the provider and GoogleModel calls.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main pydantic-ai 2.x compatibility fix and uses Conventional Commits format.
Description check ✅ Passed The description clearly explains the changes, issue context, and test results, but it omits the template's Type of Change and Checklist sections.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/agent-run-result-usage-property

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Updates pydantic-ai compatibility for current releases: agent usage accounting reads the result usage property, and Vertex model creation uses GoogleCloudProvider. The executed compatibility check against the locked pydantic-ai 2.24.0 dependency confirmed that the updated usage and Vertex construction paths complete successfully. No defects were found.

Confidence Score: 5/5

T-Rex T-Rex Logs

What T-Rex did

  • Ran the executable Pydantic AI migration validation script against pydantic-ai==2.24.0 and confirmed that the superseded usage raises TypeError while the migrated usage and Vertex wiring complete with VALIDATION_OK.
  • Documented the before-and-after verification flow, noting the exact script source and the after-output log that shows all checks passing with exit code 0.

View all artifacts

T-Rex Ran code and verified through T-Rex

Reviews (1): Last reviewed commit: "test: pin the Vertex branch of GooglePro..." | Re-trigger Greptile

@sonarqubecloud

sonarqubecloud Bot commented Aug 5, 2026

Copy link
Copy Markdown

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants