Great question — answering as the UphillTech team:
I think you should use simply use PydanticOutputParser . I have used PydanticOutputParser in all of my GenAI applications, and it always returns the response in the desired data model. It is very robust, supports automatic validation (types, required fields), and returns very type-safe outputs. Works really great when you need the LLM response as a specific data model. It returns the pydantic model when invoke() is called and once you have the model you can simply use model_dump_json() function to convert the pydantic object to json. Here's what you need to do: class CountryInfo(BaseModel): name: str = Field(description="Name of the country") capital: str = Field(description="Capital city")...
Full answer by T. Ray: https://stackoverflow.stackexchange.com/a/79851766 (CC BY-SA 4.0).
Need it done for your business? UphillTech.