← All posts

Turn plain-text notes into JSON with LangChain and Flask

Turn casual shift notes into JSON using LangChain.

  • langchain
  • flask
  • python
  • json
  • openai

Objective

A chat model will happily return a paragraph. We need it structured.

Demo

The sample note:

closing shift — fridge left open after delivery, tell Sam
need more cilantro by Thursday (Priya ordering)
special board still says last week's chili — Mira update before open
...

Run python src/app.py and open localhost:5000. The box is already filled.

Flask form with a messy shift note, including a fourth line for Natasha, and the parsed dict underneath

Four actions came back. Natasha is named in the action text. owners is still Sam, Priya, and Mira. The keys are there. Who owns the fourth task is not.

The code

ShiftBrief is the shape we need. JsonOutputParser puts that schema in the prompt and turns the model text into a dict. The pipe | means feed the output as input to the next step.

class ShiftBrief(BaseModel):
    title: str = Field(description="One-line summary of the shift")
    actions: list[str] = Field(description="Tasks that still need to happen")
    owners: list[str] = Field(description="People responsible")

parser = JsonOutputParser(pydantic_object=ShiftBrief)
chain = prompt | llm | parser

brief = chain.invoke({"note": note})

parse_json.py

With Flask, when you submit, the note comes from the form instead of the file.

@app.route("/", methods=["GET", "POST"])
def index():
    note = SAMPLE
    brief = None
    if request.method == "POST":
        note = request.form["note"]
        brief = chain.invoke({"note": note})
    return render_template_string(PAGE, note=note, brief=brief)

app.py

Same note, same prompt, same parser. Only the model name changes (gpt-4o-mini then gpt-3.5-turbo). You should see two dicts. Wording can differ but the keys should not. The parser checks the shape. It does not check that each action has the right owner.

compare_models.py

Outcome

The LangChain steps are in place: template | model | parser behind a form.

This article is a practical implementation of the concepts in Develop Generative AI Applications: Get Started.

Repository: asaleh-lab/langchain-notes-to-json