Skip to content

Fix deprecated kwargs validation to prevent silent failures #2047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

svilupp
Copy link
Contributor

@svilupp svilupp commented Jun 21, 2025

  • Replace .get() with .pop() for deprecated kwargs in Agent methods
  • Add validation to raise UserError for unknown kwargs after processing deprecated ones
  • Prevents silent acceptance of invalid kwargs like usage_limits in Agent()
  • Applied fix to Agent.init, run, run_sync, iter, and run_sync methods
  • Added tests for validation behavior
  • Maintains backward compatibility for legitimate deprecated kwargs

Fixes: #1987

svilupp added 3 commits June 21, 2025 10:45
- Replace .get() with .pop() for deprecated kwargs in Agent methods
- Add validation to raise UserError for unknown kwargs after processing deprecated ones
- Prevents silent acceptance of invalid kwargs like usage_limits in Agent()
- Applied fix to Agent.__init__, run, run_sync, iter, and run_sync methods
- Added comprehensive tests for validation behavior
- Maintains backward compatibility for legitimate deprecated kwargs

Fixes: pydantic#1987
@svilupp svilupp changed the title [Devin] Fix deprecated kwargs validation to prevent silent failures Fix deprecated kwargs validation to prevent silent failures Jun 21, 2025
Add type: ignore[call-arg] comments to test lines that intentionally
pass invalid kwargs to bypass type checker while preserving runtime
validation tests.
@svilupp
Copy link
Contributor Author

svilupp commented Jun 24, 2025

The failure is unrelated to the PR (it was there before):

Run uv run strict-no-cover
strict-no-cover v0.1.1
❎ 8 lines wrongly marked with 'pragma: no cover' are covered
pydantic_ai_slim/pydantic_ai/agent.py:298 to 299
pydantic_ai_slim/pydantic_ai/agent.py:308 to 309
pydantic_ai_slim/pydantic_ai/agent.py:315 to 316
pydantic_ai_slim/pydantic_ai/agent.py:324 to 325
Error: Process completed with exit code 1.

@DouweM
Copy link
Contributor

DouweM commented Jun 24, 2025

@svilupp Can you please fix them regardless? They're only showing up now because we only validate coverage when a method is actually edited.

@svilupp
Copy link
Contributor Author

svilupp commented Jun 25, 2025

@svilupp Can you please fix them regardless? They're only showing up now because we only validate coverage when a method is actually edited.

I tried and it throws an error that coverage is not 100% and two lines are missing (299, 325) -- which marks the block that should raise TypeError (not related to this PR).

If I add the no-cover shield back, I get:

Run uv run strict-no-cover
strict-no-cover v0.1.1
❎ 4 lines wrongly marked with 'pragma: no cover' are covered
pydantic_ai_slim/pydantic_ai/agent.py:300 to 301
pydantic_ai_slim/pydantic_ai/agent.py:326 to 327

Not sure what the best way to proceed is -- I could set up the tests for those lines, but it's unrelated to the PR...

@@ -415,6 +417,20 @@ def merge_json_schema_defs(schemas: list[dict[str, Any]]) -> tuple[list[dict[str
return rewritten_schemas, all_defs


def validate_no_deprecated_kwargs(_deprecated_kwargs: dict[str, Any]) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we drop deprecated from the names and description? If there are remaining items in the dict here, they wouldn't be deprecated kwargs, just unknown kwargs.

@DouweM
Copy link
Contributor

DouweM commented Jun 25, 2025

@svilupp I fixed the coverage issue by moving the pragma statement to the if block wrapping the TypeError!

@svilupp
Copy link
Contributor Author

svilupp commented Jun 26, 2025

@svilupp I fixed the coverage issue by moving the pragma statement to the if block wrapping the TypeError!

Thank you!

I've updated the function name and args as suggested.

@DouweM DouweM merged commit 90fc3fd into pydantic:main Jun 27, 2025
18 checks passed
@DouweM
Copy link
Contributor

DouweM commented Jun 27, 2025

@svilupp Thanks you!

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

Successfully merging this pull request may close these issues.

Gotcha: usage_limits must be in run* function
7 participants