Live data from Hacker News

Every Way to Get Structured Output from LLMs

boundaryml.com

51–60 of 89 posts

Re: Every Way to Get Structured Output from LLMs

#51
post #50
post #46

Earlier quoted context omitted.

My bad, I think I didnt explain correctly. Basically you have two options when a "," is missing (amongst other issues) in an LLM output which causes a parsing issue: - retry the request, which may take 30+ secs (if your LLM outputs are really long and you're using something like gpt4) - fix the parsing issue In our library we do the latter. The conversion from BAML types to Pydantic ones is a compile-time step unrela…

Thanks for the clarification. How do you handle dynamic types, ie types determined at runtime?

we recently added dynamic type support with this snippet! (docs coming soon!)

Python: https://github.com/BoundaryML/baml/blob/413fdf12a0c8c1ebb75c...

Typescript: https://github.com/BoundaryML/baml/blob/413fdf12a0c8c1ebb75c...

Snippet:

async def test_dynamic():

    tb = TypeBuilder()

    tb.Person.add_property("last_name", tb.string().list())

    tb.Person.add_property("height", tb.float().optional()).description(
        "Height in meters"
    )


    tb.Hobby.add_value("chess")

    for name, val in tb.Hobby.list_values():
        val.alias(name.lower())

    tb.Person.add_property("hobbies", tb.Hobby.type().list()).description(
        "Some suggested hobbies they might be good at"
    )

    # no_tb_res = await b.ExtractPeople("My name is Harrison. My hair is black and I'm 6 feet tall.")
    tb_res = await b.ExtractPeople(
        "My name is Harrison. My hair is black and I'm 6 feet tall. I'm pretty good around the hoop.",
        {"tb": tb},
    )

    assert len(tb_res) > 0, "Expected non-empty result but got empty."

    for r in tb_res:
        print(r.model_dump())

Re: Every Way to Get Structured Output from LLMs

#53
post #15

This is an article written by BAML that shows BAML as the best. Also, BAML seems to be a commercial product with no clear pricing. > Our paid capabilities only start if you use Boundary Studio, which focuses on Monitoring, Collecting Feedback, and Improving your AI pipelines. Contact us for pricing details at contact_boundaryml.com

Author here! I very deliberately avoided making that claim; the table is actually very unsorted right now, in no small part because all the solutions in the space satisfy a very different set of usage criteria - some folks use Python, others use TS, yet others want Golang or Java or something else; some want support for Ollama/llama.cpp/vLLM, others are looking for OpenAI/Anthropic support. That being said, if you ha…

[deleted]

Re: Every Way to Get Structured Output from LLMs

#54
I was recently researching structured output generation for my project and I enjoyed using Outlines library a lot. It felt quite fast as it uses FSM and indexing. There are few fine prints though:

1. Sometimes constraints can decrease the quality of the output since syntax of the response is prioritized more than quality of the response 2. For memory constrained inferences, certain sampling strategies like top-k can cause OOM errors if the max_token is too high. I haven't tested that it is entirely due to structured generation but I suppose it is possible for certain regexes. 3. Vision models and other multi-modal models are not supported yet.

Apart from this, closed models also have json output but I am not sure how consistent they are

1. https://platform.openai.com/docs/guides/text-generation/json... 2. https://docs.anthropic.com/en/docs/build-with-claude/tool-us... 3. https://ai.google.dev/gemini-api/docs/api-overview#json

Re: Every Way to Get Structured Output from LLMs

#56
post #50

Earlier quoted context omitted.

Thanks for the clarification. How do you handle dynamic types, ie types determined at runtime?

we recently added dynamic type support with this snippet! (docs coming soon!) Python: https://github.com/BoundaryML/baml/blob/413fdf12a0c8c1ebb75c... Typescript: https://github.com/BoundaryML/baml/blob/413fdf12a0c8c1ebb75c... Snippet: async def test_dynamic(): tb = TypeBuilder() tb.Person.add_property("last_name", tb.string().list()) tb.Person.add_property("height", tb.float().optional()).description( "Height in mete…

Neat, thanks! I'm still pondering wether I should be using this since most of the retries I have to do are because of the LLM itself not understanding the schema asked for (eg output with missing fields / using a value not present in `Literal[]`) — certain models being especially bad with deeply nested schemas and output gibberish. Anything on your end that can help with that?

Re: Every Way to Get Structured Output from LLMs

#57
post #56

Earlier quoted context omitted.

we recently added dynamic type support with this snippet! (docs coming soon!) Python: https://github.com/BoundaryML/baml/blob/413fdf12a0c8c1ebb75c... Typescript: https://github.com/BoundaryML/baml/blob/413fdf12a0c8c1ebb75c... Snippet: async def test_dynamic(): tb = TypeBuilder() tb.Person.add_property("last_name", tb.string().list()) tb.Person.add_property("height", tb.float().optional()).description( "Height in mete…

Neat, thanks! I'm still pondering wether I should be using this since most of the retries I have to do are because of the LLM itself not understanding the schema asked for (eg output with missing fields / using a value not present in `Literal[]`) — certain models being especially bad with deeply nested schemas and output gibberish. Anything on your end that can help with that?

nothing specific, but you can try our prompt / datamodel out on https://www.promptfiddle.com

or if you're open to share your prompt / data model with, I can send over my best guess of a good prompt! We've found these models works even with over 50+ fields / nested and whatnot decently well!

Re: Every Way to Get Structured Output from LLMs

#58
post #11

Did I understand the documentation for many of these libraries correctly in that they reprompt until they receive valid JSON? If so I don't understand why one would do that when token masking is a deterministicly verifyable way to get structured output of any kind (as done by Guidance and LMQL for instance). This is not meant to be snarky, I really am curious. Is there an upside to reprompting - aside from easier imp…

For local models you can use grammars to constrain it directly.

Re: Every Way to Get Structured Output from LLMs

#60
post #56

Earlier quoted context omitted.

Neat, thanks! I'm still pondering wether I should be using this since most of the retries I have to do are because of the LLM itself not understanding the schema asked for (eg output with missing fields / using a value not present in `Literal[]`) — certain models being especially bad with deeply nested schemas and output gibberish. Anything on your end that can help with that?

nothing specific, but you can try our prompt / datamodel out on https://www.promptfiddle.com or if you're open to share your prompt / data model with, I can send over my best guess of a good prompt! We've found these models works even with over 50+ fields / nested and whatnot decently well!

I might share it with you later on your discord server.

> I can send over my best guess of a good prompt!

Now if you could automate the above process by "fitting" a first draft prompt to a wanted schema, ie where your library makes a few adjustments if some assertions do not pass by have having a chat of its own with the LLM, that would be super useful! Heck i might just implement it myself.

Post reply on HN