Skip to content

Navigation Menu

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

Appearance settings

how to add a custom object field to request body's model with arbitrary_types_allowed=True? #13716

Unanswered
IterableTrucks asked this question in Questions
Discussion options

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.json_schema import SkipJsonSchema


class Foo:
    def __init__(self, name: str):
        self.name = name


app = FastAPI()


class Item(BaseModel, arbitrary_types_allowed=True):
    foo: SkipJsonSchema[Foo | None] = None
    bar: str


@app.post('/')
async def create_item(item: Item) -> Item:
    return item

Description

After upgrading to 0.115.12 from 0.112.4 the API cannot serve normally with a custom object field in its request body. The error is:

pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'arbitrary_request_field.Foo'>. Set arbitrary_types_allowed=True in the model_config to ignore this error or implement __get_pydantic_core_schema__ on your type to fully support it.

If you got this error by calling handler() within __get_pydantic_core_schema__ then you likely need to call handler.generate_schema(<some type>) since we do not call __get_pydantic_core_schema__ on <some type> otherwise to avoid infinite recursion.

For further information visit https://errors.pydantic.dev/2.11/u/schema-for-unknown-type

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.115.12

Pydantic Version

2.11.4

Python Version

3.13.2

Additional Context

No response

You must be logged in to vote

Replies: 1 comment · 1 reply

Comment options

After upgrading to 0.115.12 from 0.112.4...

Was the code that you wrote working correctly before you upgraded to 0.115.12?

how to add a custom object field to request body's model with arbitrary_types_allowed=True

Here's a workaround that you can use which will work, just convert your Foo class to Pydantic BaseModel like so:

from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.json_schema import SkipJsonSchema


class Foo(BaseModel):
    name: str


app = FastAPI()


class Item(BaseModel):
    foo: SkipJsonSchema[Foo | None] = None
    bar: str


@app.post('/')
async def create_item(item: Item) -> Item:
    return item
  1. I saved the code in a file named main.py and ran it using uvicorn main:app --reload.
  2. I invoked the API using cURL:
curl -X POST -H "Content-Type: application/json" -d '{"bar": "hello from curl"}' http://127.0.0.1:8000/
{"foo":null,"bar":"hello from curl"}

curl -X POST -H "Content-Type: application/json" -d '{"foo": {"name": "example foo"}, "bar": "another value"}' http://127.0.0.1:8000/
{"foo":{"name":"example foo"},"bar":"another value"}%
You must be logged in to vote
1 reply
@IterableTrucks
Comment options

The code I wrote works correctly in 0.112.4. The custom object is a class imported from another project so I can't just rewrite it to Pydantic Model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.