Skip to content

fix(pydantic): improve error message for untyped state parameter#614

Merged
skrawcz merged 1 commit intoapache:mainfrom
majiayu000:fix/385-pydantic-uninformative-error
Dec 31, 2025
Merged

fix(pydantic): improve error message for untyped state parameter#614
skrawcz merged 1 commit intoapache:mainfrom
majiayu000:fix/385-pydantic-uninformative-error

Conversation

@majiayu000
Copy link
Contributor

Summary

  • Fixed uninformative KeyError when pydantic_action has untyped state parameter
  • Now raises clear ValueError explaining the state parameter must be annotated

Changes

  • Changed type_hints["state"] to type_hints.get("state") to avoid KeyError
  • Updated error message to clearly state: "'state' parameter must be annotated with a type extending pydantic.BaseModel"

Test plan

  • Added test function _fn_with_untyped_state_arg and parameterized test case
  • Added test__validate_and_extract_signature_types_untyped_state_error_message to verify error message
  • All existing pydantic tests pass (32 tests)
  • Pre-commit checks pass

Closes #385

When a pydantic_action function has a 'state' parameter without type
annotation, raise a clear ValueError explaining that the parameter must
be annotated with a pydantic.BaseModel subclass, instead of a cryptic
KeyError.

Closes apache#385

Signed-off-by: majiayu000 <1835304752@qq.com>
"a type annotation of a type extending: pydantic.BaseModel. Got parameter "
"state: {state_model.__qualname__}."
"The 'state' parameter must be annotated with a type extending pydantic.BaseModel. "
f"Got: {state_model}."
Copy link
Contributor

Choose a reason for hiding this comment

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

can you show a before and after example here to help understand the change here please?

Copy link
Contributor

@skrawcz skrawcz left a comment

Choose a reason for hiding this comment

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

looks good. would love a before and after to show case it.

@majiayu000
Copy link
Contributor Author

Before and After Example

Before (without this fix)

from burr.integrations.pydantic import pydantic_action

@pydantic_action
def my_action(state):  # missing type annotation
    return state

Error:

KeyError: 'state'

This error is cryptic and doesn't tell the user what went wrong.


After (with this fix)

from burr.integrations.pydantic import pydantic_action

@pydantic_action
def my_action(state):  # missing type annotation
    return state

Error:

ValueError: The 'state' parameter must be annotated with a type extending pydantic.BaseModel.

Now the error message clearly explains what the user needs to fix.

@skrawcz skrawcz merged commit 1a92593 into apache:main Dec 31, 2025
10 of 11 checks passed
@skrawcz
Copy link
Contributor

skrawcz commented Dec 31, 2025

Great thanks!

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.

pydantic: uninformative error

2 participants