Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Json fields are not transitive when copied across models #12587

Open
@lucid-ben-sizer

Description

@lucid-ben-sizer

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

If I have a Json field in a model, it requires string-like input and converts it to something else (presumably a dict of whatever). But if I then need to create a new model with input from the first, it will fail because a Json field does not accept another Json field, only a string.

The reason why I might do this is because I could have a generic pipeline that expects to receivesome Python object and then automatically converts it to a Pydantic schema object of my choosing. It stands to reason that I should, if possible, be able to provide exactly the right schema object required, and it can be passed through as-is. This is the case for any other schema object I try, just not ones containing a Json field.

FastAPI appears to use exactly this mechanism - annotate a path with a Pydantic schema, and it'll attempt to create the object for you - which is why I have hit this problem. I can't return an object of the type I want because it's already got 'loaded' JSON in it and fails validation when creating the response.

It appears there might be special case code inside model_validate because the process works there, just not when creating new objects directly.

I expect this is related to#11154 .

Example Code

frompydanticimportBaseModel,JsonclassSimpleSchemaContainingNoJSON(BaseModel):name:strnumber:intclassSimpleSchemaContainingJSON(BaseModel):name:strdocument:Jsondeftest_schema_without_json():""" Check that a schema can usually be loaded into a new instance without trouble. """val_1=SimpleSchemaContainingNoJSON(name="Name",number=123)val_2=SimpleSchemaContainingNoJSON(name=val_1.name,number=val_1.number)assertval_2isnotNone# succeedsdeftest_schema_with_json():""" Check whether a schema containing JSON can be loaded into a new instance. """val_1=SimpleSchemaContainingJSON(name="Name",document="{}")val_2=SimpleSchemaContainingJSON(name=val_1.name,document=val_1.document)# line above raises pydantic_core._pydantic_core.ValidationError - JSON input should be string, bytes or bytearrayassertval_2isnotNone# code never reaches heredeftest_schema_with_json_model_validate():""" Check whether a schema containing JSON can be loaded into a new instance via model_validate instead. """val_1=SimpleSchemaContainingJSON(name="Name",document="{}")val_2=SimpleSchemaContainingJSON.model_validate(val_1)assertval_2isnotNone# succeeds

Python, Pydantic & OS Version

pydantic version: 2.12.4        pydantic-core version: 2.41.5          pydantic-core build: profile=release pgo=false               python version: 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)]                     platform: Windows-11-10.0.26200-SP0             related packages: fastapi-0.122.0 pydantic-settings-2.10.1 typing_extensions-4.15.0                       commit: unknown

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp