I’m using Langchain following:
class MyOutput(BaseModel):
my_date: str | None
my_name: str | None
@classmethod
def run(cls, data: str) -> MyOutput:
cls.set_llm_instance()
output_parser = PydanticOutputParser(pydantic_object=MyOutput)
chain = (prompt_template() | cls.llm | output_parser)
answer = chain.invoke({"data": data, "format_instructions": output_parser.get_format_instructions()})
where llm is a BaseChatModel, and prompt_template is a function I have in a different file with:
def prompt_template() -> PromptTemplate:
question_template = config_parameters["prompt_metadata"]
prompt_ = PromptTemplate(
input_variables=["data"],
template=question_template,
)
return prompt_
If I ommit introducing output_parser and remove the key “format_instructions” from the chain.invoke I get that the AI got correctly the prompt, and answer is an AIMessage with content (e.g.,):
‘jsonn{n "DATE": "01/01/2019",n "NAME": "1045"n}n
‘
However, if I put the output_parser it’s always None for my_date and my_name. I tried to put a Field with Description for both pydantic properties describing what are they, although saying that they can be None too (my json could be None for Date, name or both depending on the prompt). However, it’s always None. How should I proceed?