Structured Output: Format Instructions vs with_structured_output #27709
Replies: 3 comments
-
Hello, I've been exploring ways to obtain structured output from LLMs and wanted to share my experience on the topic. While the For example, you could use a prompt like the following: Please extract all URLs and dates from the following user input.
The response must be a JSON array of objects, each containing a "url" and "date" field.
## OUTPUT - YOU MUST ALWAYS RESPOND IN THIS JSON FORMAT
[
{"url": "<detected URL>", "date": "<detected date>"},
// Repeat for each URL and date found
]
Return the response as a plain JSON string without any formatting or code blocks. I only need the raw JSON data in a single-line format.
By specifying the format and reinforcing it in the prompt, the LLM is more likely to produce the structured output you need. Moreover, one of the advantages of this approach is that you can use LangChain's Prompt Templates to make the JSON structure or certain elements within it variables. This allows you to dynamically generate prompts based on different inputs or contexts, making your solution more flexible and reusable. For example: from langchain_core.prompts import PromptTemplate
prompt_template = PromptTemplate.from_template(
'''Please extract all {entity_type} from the following user input.
The response must be a JSON array containing all detected {entity_type}.
## OUTPUT - YOU MUST ALWAYS RESPOND IN THIS JSON FORMAT
[
"{{ {entity_type}: \"<detected {entity_type}>\" }}",
// Repeat for each {entity_type} found
]
Return the response as a plain JSON string without any formatting or code blocks. I only need the raw JSON data in a single-line format.
''')
prompt = prompt_template.invoke({"entity_type": "URLs"}) In this example, the Additionally, I wanted to mention that there is an experimental class called You can find more information here: LangChain Documentation - JSONFormer This could be another avenue to explore for achieving robust structured outputs. Ultimately, the choice between using methods like |
Beta Was this translation helpful? Give feedback.
-
Hi, Not sure if you still are interested in the details of structured outputs, I have a blog post on what I understand about it. Let me know if you find it useful or have any questions. @torrresagus I hadn't heard of JSONFormer, looks interesting but I don't fully understand what it is doing. How does this compare to JSON mode which constrains generation to JSON only? |
Beta Was this translation helpful? Give feedback.
-
Dear Andrew,
I will for sure check it out.
Regards.
…On Tue, Feb 4, 2025, 6:31 AM Andrew Docherty ***@***.***> wrote:
Hi, Not sure if you still are interested in the details of structured
outputs, I have a blog post on what I understand about it. Let me know if
you find it useful or have any questions.
***@***.***/mastering-structured-output-in-llms-choosing-the-right-model-for-json-output-with-langchain-be29fb6f6675
@torrresagus <https://github.com/torrresagus> I hadn't heard of
JSONFormer, looks interesting but I don't fully understand what it is
doing. How does this compare to JSON mode which constrains generation to
JSON only?
—
Reply to this email directly, view it on GitHub
<#27709 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJ7PKR4ZZW6ILF56I2OCRBT2OAX7ZAVCNFSM6AAAAABQZ3E7PSVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEMBVGAZDKMA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello All,
I am interested in having a robust way to get structured output from the LLM. I have seen that with_structured_output uses internally bind or bind_tools. However, other methods use the format instructions and provide them to the LLM. which is better and more robust?
Beta Was this translation helpful? Give feedback.
All reactions