Replies: 1 comment 2 replies
-
Hey @pleelapr! I'm here to help you with your LangChain issue. Let's get this sorted out together! To ensure your chain works correctly with the
Here is the updated code: from langchain.pydantic_v1 import BaseModel, Field
from langchain_core.output_parsers.retry import RetryOutputParser
from langchain_core.output_parsers.pydantic import PydanticOutputParser
from langchain_core.chains import RunnableConfig
class Triplet(BaseModel):
subject: str
relation: str
object: str
class TripletExtraction(BaseModel):
extraction_list: List[Triplet] = Field(description="List of triplets extracted from the conversation")
def _initialize_chain(self):
parser = PydanticOutputParser(pydantic_object=TripletExtraction)
retry_parser = RetryOutputParser.from_llm(
parser=parser, llm=self._model.with_structured_output(TripletExtraction), max_retries=3
)
# Initialize the chain with the prompt and the model
chain = self._prompt | self._model.with_structured_output(TripletExtraction, include_raw=True) | retry_parser
# Add config to the chain and return the chain
return chain.with_config(RunnableConfig(tags=[self._agent_prefix], metadata={"agent": self._agent_prefix})) Explanation:
This setup ensures that the For more details on handling parsing errors and retry mechanisms, you can refer to the LangChain documentation on RetryOutputParser [1][2][3][4]. |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I am trying to create a chain using LCEL for OpenAI using with_structured_output on pydantic BaseModel.
However, there are some cases where the LLM generated the wrong structure. Hence, I would like to use the RetryOutputParser. However, the graph seems to stuck, and I don't know which part of the code I need to change.
System Info
langchain==0.2.5
langchain-chroma==0.1.1
langchain-community==0.2.5
langchain-core==0.2.9
langchain-experimental==0.0.61
langchain-google-vertexai==1.0.5
langchain-openai==0.1.8
langchain-postgres==0.0.9
langchain-text-splitters==0.2.1
langchainhub==0.1.14
platform (mac)
python version 3.11.5
Beta Was this translation helpful? Give feedback.
All reactions