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
frompydanticimportBaseModel, FieldclassJoke(BaseModel):
"""Joke to tell user."""setup: str=Field(description="The setup of the joke")
punchline: str=Field(description="The punchline to the joke")
rating: int=Field(description="How funny the joke is, from 1 to 10")
parser=PydanticOutputParser(pydantic_object=Joke)
prompt=ChatPromptTemplate.from_messages(
[
(
"system",
"Answer the user query. Wrap the output in `json` tags\n{format_instructions}",
),
("human", "Tell me a joke about cats"),
]
).partial(format_instructions=parser.get_format_instructions())
structured_llm=prompt|llm|parserstructured_llm.invoke("Tell me a joke about cats")
Description
When I try the example above of JSON output using output parsers, this often fails compared to using the prompt in user mode, such as the following code:
prompt_user_format=ChatPromptTemplate.from_template(
"{input} \n{format_instructions}"
).partial(format_instructions=parser.get_format_instructions())
structured_llm=prompt_user_format|llm_model|parserstructured_llm.invoke("Tell me a joke about cats")
My question is why are most models failing to output JSON output with the system prompt (as in the reference page) but succeed when using a user prompt? Is there something I'm missing in my code?
From my testing, see table below, on Ollama models while Gemma2 works better with a system prompt, other models such as fail to output JSON when using the system model.
The table below shows the percentage of 20 runs that return conforming JSON in LangChain. For reference I'm using Ollama local models, all smaller than 9B parameters.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Checked other resources
Commit to Help
Example Code
In the example outputting JSON formatted objects on this page:
https://python.langchain.com/docs/how_to/structured_output/#custom-parsing
with the following code:
Description
When I try the example above of JSON output using output parsers, this often fails compared to using the prompt in user mode, such as the following code:
My question is why are most models failing to output JSON output with the system prompt (as in the reference page) but succeed when using a user prompt? Is there something I'm missing in my code?
From my testing, see table below, on Ollama models while Gemma2 works better with a system prompt, other models such as fail to output JSON when using the system model.
The table below shows the percentage of 20 runs that return conforming JSON in LangChain. For reference I'm using Ollama local models, all smaller than 9B parameters.
Full code to produce the table is available here:
https://github.com/adocherty/mastering-structured-output/blob/main/2-langchain-structured-output-evaluation.ipynb
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies
Beta Was this translation helpful? Give feedback.
All reactions