Skip to content

Ensure custom __init__() is called when using model_validate_strings()#12897

Merged
Viicos merged 2 commits intopydantic:mainfrom
siewcapital:fix-model-validate-strings-custom-init
Mar 25, 2026
Merged

Ensure custom __init__() is called when using model_validate_strings()#12897
Viicos merged 2 commits intopydantic:mainfrom
siewcapital:fix-model-validate-strings-custom-init

Conversation

@siewcapital
Copy link
Copy Markdown
Contributor

Summary

model_validate_strings() currently skips the custom __init__ path for models because StringMapping always returns None from Input::as_kwargs().

This causes behavior differences compared to model_validate() / model_validate_json() for models that override __init__, and is the root issue reported in #12718.

Changes

  • Return mapping kwargs from StringMapping::as_kwargs() when the input is a mapping.
  • Keep None for non-mapping (String) values.
  • Add regression test ensuring model_validate_strings() invokes a custom __init__ and forwards string mapping values.

Tests

  • uv run pytest tests/test_edge_cases.py -k "custom_init or model_validate_strings_custom_init_called or init_inspection"
  • uv run pytest tests/test_main.py -k "model_validate_strings"

Closes #12718

@github-actions github-actions Bot added the relnotes-fix Used for bugfixes. label Mar 6, 2026
Copy link
Copy Markdown
Collaborator

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks, implementation seems correct however I would like the test to be unified with existing one please.

Comment thread tests/test_edge_cases.py Outdated
@Viicos Viicos closed this Mar 25, 2026
@Viicos Viicos reopened this Mar 25, 2026
@Viicos Viicos changed the title Fix custom __init__ path for model_validate_strings Ensure custom __init__() is called when using model_validate_strings() Mar 25, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 25, 2026

Merging this PR will improve performance by 5.22%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 2 improved benchmarks
✅ 210 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_complete_core_isinstance 1.6 ms 1.5 ms +5.22%
test_complete_core_error 1.6 ms 1.5 ms +5.12%

Comparing siewcapital:fix-model-validate-strings-custom-init (16dafbf) with main (0574aaa)

Open in CodSpeed

@github-actions
Copy link
Copy Markdown
Contributor

Coverage report

This PR does not seem to contain any modification to coverable code.

@Viicos Viicos merged commit 1a37a3d into pydantic:main Mar 25, 2026
81 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

relnotes-fix Used for bugfixes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

model_validate_strings() does not respect custom __init__()

4 participants