You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello. Apologies if this is a duplicate, but I couldn't find anything super relevant to what I'm trying to do. I've created a reproducible example here.
The idea is that I want to be able to send a proper exception from the chain -- a 422 when something isn't relevant. Note that this is a toy example to illustrate the point, but I want to do this on a larger chain I'm working on.
When I use an HTTPException, I get the error TypeError: Type is not JSON serializable: HTTPException, which makes sense to me since the final output would take a dict, str, or JSON structure. But is there a way/workaround to make this work that I may be missing?
If the link somehow isn't accessible, here is the chain.
importjsonfromtypingimportDictfromdotenvimportload_dotenvfromlangchain.promptsimportPromptTemplatefromlangchain_core.output_parsersimportJsonOutputParserfromlangchain_core.pydantic_v1importBaseModel, Fieldfromlangchain_core.runnablesimportRunnableLambdafromlangchain_openaiimportChatOpenAIfromfastapiimportHTTPExceptionload_dotenv()
model=ChatOpenAI(model="gpt-3.5-turbo")
classFruitModel(BaseModel):
shape: str=Field(
...,
description="The shape of the fruit",
)
color: str=Field(
...,
description="The color of the fruit",
)
taste: str=Field(
...,
description="The taste of the fruit"
)
template="You are describing a {fruit}. Please provide the name, shape, and color of the fruit as per the {format_instructions}."parser=JsonOutputParser(pydantic_object=FruitModel)
defpost_process_result(result):
result=json.loads(f"result")
ifnotresult["is_fruit"]:
raiseHTTPException(status_code=400, detail="The object is not a fruit.")
else:
returnresultprompt=PromptTemplate(
template=template,
input_variables=["fruit"],
partial_variables={
"format_instructions": parser.get_format_instructions()
}
)
defis_input_numeric(input):
return {
"input": input,
"result": input.isnumeric()
}
chain=RunnableLambda(is_input_numeric).with_types(input_type=str, output_type=Dict) |RunnableLambda(
lambdax: HTTPException(
status_code=422,
detail=f"{x['input']} is not string."
) ifx["result"] elseRunnableLambda(
lambdax: x["input"]
).with_types(
output_type=str
) |prompt|model|parser
)
The text was updated successfully, but these errors were encountered:
Hello. Apologies if this is a duplicate, but I couldn't find anything super relevant to what I'm trying to do. I've created a reproducible example here.
The idea is that I want to be able to send a proper exception from the chain -- a
422
when something isn't relevant. Note that this is a toy example to illustrate the point, but I want to do this on a larger chain I'm working on.When I use an
HTTPException
, I get the errorTypeError: Type is not JSON serializable: HTTPException
, which makes sense to me since the final output would take adict
,str
, orJSON
structure. But is there a way/workaround to make this work that I may be missing?If the link somehow isn't accessible, here is the chain.
The text was updated successfully, but these errors were encountered: