我正在处理数据生成并使用 Pydantic 类来执行此操作。数据将用于调用 REST API,因此模型与解析代码如下所示:
class NEFDataModel(BaseModel):
request: str = Field(description="A question about utilizing the NEF API to perform a specific action.")
api_call: str = Field(description="The full URL (taken from the 'path' field) of the API endpoint being invoked.")
description: str = Field(description="A brief summary of the purpose of the API endpoint.")
method: str = Field(description="The HTTP method used to call the API endpoint.")
operation: str = Field(description="The operationId of the API endpoint.")
parameters: dict = Field(description="Any parameters required by the API endpoint, a comma separated dict of key-value pairs.")
nef_output_parser = PydanticOutputParser(pydantic_object=NEFDataModel)
....
output_dict = nef_output_parser.parse(response.content)
但是,我在调用
nef_output_parser.parse(response.content)
时收到以下错误:
NEFDataModel expected dict not list (type=type_error)
如果我从调用的链中打印
response.content
(不调用解析器),它看起来像这样:
[
{
"request": "How can I obtain an access token for future requests?",
"api_call": "/api/v1/login/access-token",
"description": "OAuth2 compatible token login, get an access token for future requests",
"method": "post",
"operation": "login_access_token_api_v1_login_access_token_post",
"parameters": {
"grant_type": "password",
"username": "your_username",
"password": "your_password",
"scope": "",
"client_id": "your_client_id",
"client_secret": "your_client_secret"
}
},
...
]
我尝试过使用数据类型,但没有用。根据我到目前为止的分析,
parameters
字段导致了这个问题。
如果
NEFDataModel
中有多个 response.content
,则需要一个 RootModel
将它们解析为列表。
from pydantic import RootModel
class NEFDataModels(RootModel):
root: list[NEFDataModel]
nef_output_parser = PydanticOutputParser(pydantic_object=NEFDataModels)