Simplest way to setup a structured output with dict

Hi, I am trying to use with_structured_output. To define a schema, the docs demonstrate a basic dict {“foo”:”bar"} which generalizes as {“field name”: “field description”}.

And usage will be:

# Define schema
schema = {"foo": "bar"}
# Bind schema to model
model_with_structure = model.with_structured_output(schema)
# Invoke the model to produce structured output that matches the schema
structured_output = model_with_structure.invoke(user_input)

Doc : Structured outputs | 🦜️🔗 LangChain

However with such an approach I get an error “{‘concepts’: ‘The list of concepts as strings’} - Functions must be passed in as Dict, pydantic.BaseModel, or Callable. If they’re a dict they must either be in OpenAI function format or valid JSON schema with top-level ‘title’ and ‘description’ keys.”

And the remainder of the same documention seems to imply that instead, the schema may need to be set as a tool.

It seems that instead, I should rely on the more complex Open AI format described in the guide : How to return structured data from a model | 🦜️🔗 LangChain

To sum it up, I couldn’t really make sense of the structured outputs documentation, with the simplest approach of just using a dict.

You should be able to use a TypedDict (see docs here).

from typing_extensions import Annotated, TypedDict

class MySchema(TypedDict):
    """Description of schema."""

    concepts: Annotated[str, ..., "The list of concepts as strings"]

let me know if I misunderstood your question. We’ll get that doc fixed as it’s a little unclear!

Hi @chester-lc , indeed I could have the TypedDict to work but I found this info in the guide rather than the documentation itself, which shows a simpler { “foo”: “bar” } schema that doesn’t seem to work, or that may be used differently (it seems that passing this schema as a tool could work, but not with with_structured_output?). So yeah it seems that something is off or not totally clear with the documentation (this page Structured outputs | 🦜️🔗 LangChain).