Raw LCEL
Build the machinery first
This is the compact version for one intermediate stage. Reusing it cleanly means extracting another helper—which is already the beginning of an orchestration library.
prompt = ChatPromptTemplate.from_messages([
("system", METHOD_EXTRACTION_SYSTEM_MESSAGE),
("human", HUMAN_MESSAGE_PROMPT),
])
primary = (
prompt
| llm
| JsonOutputParser(
pydantic_object=MethodExtractionOutput,
)
)
fallback = prompt | llm | StrOutputParser()
method_layer = primary.with_fallbacks(
[fallback],
exceptions_to_handle=PARSE_ERRORS,
)
pipeline = RunnablePassthrough.assign(
method_json_output=method_layer,
)
state = pipeline.invoke(inputs)