Stream: python
Topic: fastAPI + fhir.resources
Eric Haas (Apr 16 2021 at 23:25):
I am trying to use fastAPI + fhir.resources. However, I am having some issues with it and the OpenAPI docs.
here is my attempt in python 3.7.3 following the basic fastAPI tutorial but substituting in the fhir.resources model:
from fastapi import FastAPI
from fhir.resources.practitioner import Period
from fhir.resources.fhirtypes import PeriodType
app = FastAPI()
@app.post("/period/", response_model=PeriodType, response_model_exclude_unset=False, response_model_exclude_none=True)
async def create_item(period: Period):
return period
When trying to execute using this object in swagger:
{
"resource_type": "Period",
"id": "string"
}
I get this error:
TypeError: dict() got an unexpected keyword argument 'exclude_unset'
Eric Haas (Apr 16 2021 at 23:27):
am I missing something obvious in fastAPI.. If we could use fastAPI and get swagger docs out of the box that would be huge.
Eric Haas (Apr 19 2021 at 15:35):
more on this, the models don't support all of Pydantic's dict() method arguments:
from fhir.resources.period import Period
from fhir.resources.fhirtypes import PeriodType
p = Period()
print(p.dict(exclude_unset=True))
Traceback (most recent call last):
File "/Users/ehaas/Documents/Python/fastAPI_tutorial/main.py", line 7, in <module>
print(p.dict(exclude_unset=True))
TypeError: dict() got an unexpected keyword argument 'exclude_unset'
[Finished in 0.33s]
Eric Haas (Apr 19 2021 at 15:49):
looking at the inheritance of Period:
[<class 'fhir.resources.period.Period'>, <class 'fhir.resources.element.Element'>, <class 'fhir.resources.fhirabstractmodel.FHIRAbstractModel'>, <class 'pydantic.main.BaseModel'>, <class 'pydantic.utils.Representation'>, <class 'abc.ABC'>, <class 'object'>]
Eric Haas (Apr 19 2021 at 16:34):
I am in over my head with python subclassing and pydantic but the FHIRBaseClass seems to be passing these arguments through so why are the models not allowing it?
Carl Anderson (Apr 19 2021 at 18:56):
I'm not sure what's wrong - but I can't recreate your issue.
can@T410-W10 ~ () $ pip3 install fhir fhir.resources
Collecting fhir
Using cached fhir-0.0.4.tar.gz (18 kB)
Building wheels for collected packages: fhir
Building wheel for fhir (setup.py) ... done
Created wheel for fhir: filename=fhir-0.0.4-py3-none-any.whl size=16555 sha256=03bc66e2374d97729a7a856d49dd2a7318ba999205b306a6173ff5ca168296d9
Stored in directory: /Users/can/Library/Caches/pip/wheels/e6/ac/1b/7fecf3a4531dd5b8129e0b07c606f940bb30633a4b95ac6023
Successfully built fhir
Installing collected packages: fhir
Successfully installed fhir-0.0.4
Collecting fhir.resources
Downloading fhir.resources-6.1.0-py2.py3-none-any.whl (2.3 MB)
|████████████████████████████████| 2.3 MB 1.6 MB/s
Collecting pydantic[email]>=1.7.2
Downloading pydantic-1.8.1-cp38-cp38-macosx_10_9_x86_64.whl (2.6 MB)
|████████████████████████████████| 2.6 MB 17.5 MB/s
Collecting typing-extensions>=3.7.4.3
Downloading typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Collecting email-validator>=1.0.3; extra == "email"
Downloading email_validator-1.1.2-py2.py3-none-any.whl (17 kB)
Requirement already satisfied: idna>=2.0.0 in /usr/local/lib/python3.8/site-packages (from email-validator>=1.0.3; extra == "email"->pydantic[email]>=1.7.2->fhir.resources) (2.10)
Collecting dnspython>=1.15.0
Downloading dnspython-2.1.0-py3-none-any.whl (241 kB)
|████████████████████████████████| 241 kB 16.7 MB/s
Installing collected packages: typing-extensions, dnspython, email-validator, pydantic, fhir.resources
Successfully installed dnspython-2.1.0 email-validator-1.1.2 fhir.resources-6.1.0 pydantic-1.8.1 typing-extensions-3.7.4.3
can@T410-W10 ~ () $ ipython
Python 3.8.5 (default, Jul 21 2020, 10:42:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:from fhir.resources.period import Period
from fhir.resources.fhirtypes import PeriodType
p = Period()
print(p.dict(exclude_unset=True))::::
:--
{}
In [2]:
Carl Anderson (Apr 19 2021 at 18:59):
However, the online docs for pydantic suggest that exclude_unset
has replaced an older, deprecated keyword skip_defaults
- so maybe your installed pydantic module is out of date...
Eric Haas (Apr 19 2021 at 19:26):
thanks @Carl Anderson I will check my environmental variables
Carl Anderson (Apr 19 2021 at 19:52):
You might try pip show pydantic
Eric Haas (Apr 19 2021 at 20:53):
I updated my environment from python 3.7.3 to latest version of python 3.9.4 and that seemed to do the trick.
Eric Haas (Apr 19 2021 at 21:01):
also tried a new 3.7.3 environment and that works too
Eric Haas (Apr 20 2021 at 16:33):
I am unable to get the swagger UI to display the resource_type on the response. I have tried several method arguments and a couple of string values but does anyone know a simple way to get it to display?
Eric Haas (Apr 20 2021 at 16:37):
for example this code:
from fastapi import FastAPI
from fhir.resources.period import Period
from fhir.resources.fhirtypes import PeriodType
app = FastAPI()
@app.post("/period/", response_model=Period, response_model_exclude_none=True)
async def create_item(period: Period):
return period
results in this:
Eric Haas (Apr 20 2021 at 16:39):
also @Md Nazrul Islam what is the difference in the the response_model being "Period_type" instead of "Period". I didn't see any difference in behavior in my simple little Swagger example?
Last updated: Apr 12 2022 at 19:14 UTC