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
Nested object discriminator field doesn't get serialized to str properly in model_dump #9235
Comments
I don't know if this is unexpected or even fixable behavior, but created a PR with failing test case. I may delve into the code and see if there is an easy fix for this and add it for your review. If the PR is unwanted, please close, I won't be offended. |
Thanks for reporting this. I don't actually know if this is a bug - why would you use a discriminator here, if the type isn't a union? Just a note - this isn't a regression in 2.7.0, but rather has been present throughout recent minor releases. |
The bug exists whether or not you use a union. I should have made that
clear.
…On Sat, Apr 13, 2024 at 16:27 Sydney Runkle ***@***.***> wrote:
@kf-novi <https://github.com/kf-novi>,
Thanks for reporting this. I don't actually know if this is a bug - why
would you use a discriminator here, if the type isn't a union?
Just a note - this isn't a regression in 2.7.0, but rather has been
present throughout recent minor releases.
—
Reply to this email directly, view it on GitHub
<#9235 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A5CBQT6PHMSSAU3RAFQKMEDY5G5P5AVCNFSM6AAAAABGE4QDWGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANJTG44TMMJZGQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Ah I see, thanks |
I did investigate a fix and quickly discovered it would be in pydantic
core. Rust and serde are not my native languages so I spun out
pretty quickly.
…On Sat, Apr 13, 2024 at 17:22 Sydney Runkle ***@***.***> wrote:
Ah I see, thanks
—
Reply to this email directly, view it on GitHub
<#9235 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A5CBQT72GQW6RI77SMGEOULY5HD3FAVCNFSM6AAAAABGE4QDWGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANJTHAYTMMRWG4>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Yep, makes sense. Especially now that we've moved the |
This reverts commit 9da09f8.
This appears to be a regression in pydantic/pydantic-core. This bug does not exist in 2.6.4 but does exist in 2.7.0. pydantic-core version for my 2.6.4 install is 2.16.3 |
Did some further investigation. I got as far back as pydantic-core 2.17.0 with this bug still in place. So, my guess is this bug was introduced in pydantic-core 2.17.0. Not sure about the particular feature or commit that introduced it. Hopefully that helps track it down! |
🤔 I rigged up pydantic-core on my dev box and started rolling back changes. The commit that causes the regression seems to be pydantic/pydantic-core@71d54a2 which is the upgrade to PyO3 0.21 beta. edit to prevent spam: PyO3 0.21.2 does not fix this issue either. Time to see if I can figure out how to debug rust code. |
I've traced the issue to In pydantic/pydantic-core@91c3541 the line in question is 125. Before pyo3 0.21.0 the "inner" value is I don't know why this is happening. I'm having fun tracking this down so I'll see if I figure it out or not. If I'm abusing the issue comments, please tell me and I'll stop posting new ones and edit the old ones. edit: As near as I can tell, the issue stems from differing behavior of |
No worries at all! Always good to have a live discussion re debugging. Ping me if you have any questions (though admittedly, @davidhewitt will be able to help much more promptly with the pyo3 upgrade stuff). |
@davidhewitt I found the behavior change! In return Ok(OutputValue::OkStr(s)); and in the upgrade to pyo3 v0.21, it was changed to return Ok(OutputValue::OkStr(py_str.clone()))
My proposed fix is return Ok(OutputValue::OkStr(PyString::new_bound(value.py(), s))); I think @kfreezen will create the PR for this one 😁 you can do with it what you will from there. edit to add: I feel slightly foolish about the sheer amount of time I spent on this tiny bug. Goes to show you how much people want that "contributor" tag 😜 |
Initial Checks
Description
If using a discriminator field for a nested object as seen in the example code, the
model_dump
function does not serialize that discriminator field fromSomeEnum
tostr
.I would have expected that discriminator field to be serialized automatically.
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: