question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

validation errors for openAPI schema with length >=2 tuples

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue 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 typing import Tuple
from fastapi import FastAPI
from pydantic import BaseModel


class FooArgs(BaseModel):
    bars: Tuple[str, str]


app = FastAPI()


@app.post("/foo")
def foo(args: FooArgs):
    ...

Description

If you try to retrieve the /openapi.json, the FooArgs get a validation error:

  File ".../lib/python3.9/site-packages/fastapi/openapi/utils.py", line 410, in get_openapi
    return jsonable_encoder(OpenAPI(**output), by_alias=True, exclude_none=True)  # type: ignore
  File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 2 validation errors for OpenAPI
components -> schemas -> FooArgs -> properties -> bars -> items
  value is not a valid dict (type=type_error.dict)
components -> schemas -> FooArgs -> $ref
  field required (type=value_error.missing)

This error is new to fastapi>=0.68, it does not happen with fastapi==0.67.0.

I think this is related to Pydantic coercing Tuple[str, str] into Dict[str, str] somewhere?

Things that don’t cause this error:

class FooArgs(BaseModel):
    bars: Tuple[str]
class FooArgs(BaseModel):
    bars: Tuple[str, ...]

This that do cause this error:

class FooArgs(BaseModel):
    bars: Tuple[str, str]
class FooArgs(BaseModel):
    bars: Tuple[int, str]
class FooArgs(BaseModel):
    bars: Tuple[str, str, str]  # etc.

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.68.1

Python Version

3.9.1 (also seeing this in 3.8.*)

Additional Context

Seems pretty likely that this commit introduced it, but there’s a lot going on so I’m not sure where:

https://github.com/tiangolo/fastapi/commit/97fa743ecb5a716fa0223a7463cb83f3f0df494c#diff-4939896f79697822446463d5d6dfbf7a0ea5805c46565d355c7305d86f3405c7

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
fakegermanocommented, Sep 17, 2021

after another bit of search, I think this issue is a duplicate of both https://github.com/tiangolo/fastapi/issues/3665 and https://github.com/tiangolo/fastapi/issues/3782, there is also some more info there!

3reactions
trevorwangcommented, Sep 17, 2021

it’s http://localhost:8000/docs

It is not working now. I got the same error as yours. Sad.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Schema generation rules · GitBook - Goswagger.Io
Validation errors : Returned errors are supported by the go-openapi/errors/Error type, which supports errors codes and composite errors.
Read more >
schema validationError FastAPI & pydantic - Stack Overflow
It looks like tuples are currently not supported in OpenAPI. There are a couple of way to work around it: Use a List...
Read more >
ytt - Data Values Schema Reference - Carvel
When a validation runs, all rules are evaluated. Rules are evaluated in the order they appear in the annotation (left-to-right). The one exception...
Read more >
Strict mode - Ajv JSON schema validator
Sometimes users accidentally create schema for unit (a tuple with one item) that only validates the first item, this restriction prevents this mistake...
Read more >
Field Types - pydantic
validation is faster since it is only attempted against one model; only one explicit error is raised in case of failure; the generated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found