6d3c1609cd12a06201a7c429e2ad1ae9fcf9749e
Structured Output with Union Events API
This repository contains a small CLI tool that demonstrates how to parse raw log lines into typed events using LangChain's structured output capabilities and Pydantic v2.
Features
- Two event models:
HttpOkEvent– successful HTTP request (status 200)HttpErrorEvent– error response (4xx/5xx)
- Uses a discriminator field (
kind) to route the parsed data into the correct model. - Parses each log line with an LLM via LangChain and returns a validated Pydantic object.
- Prints a simple table of all parsed events.
Requirements
langchain-core>=1.0
langchain-openai
pydantic>=2
python-dotenv
Install them with:
pip install -r requirements.txt
Note
: The script uses the
gpt-4o-minimodel by default. Set the environment variableOPENAI_API_KEYto your key.
Usage
From a file
python main.py --file logs.txt
From stdin
echo -e "GET /api/v1/users 200 123ms\nPOST /api/v1/login 404 Not Found" | python main.py
The output will look like:
kind | path | status | duration_ms | error_message
-----+---------------+--------+-------------+--------------
ok | /api/v1/users | 200 | 123 |
error| /api/v1/login | 404 | | Not Found
How it works
The script builds a LangChain chain that:
- Prompts the LLM to parse a single log line into JSON.
- Uses
PydanticOutputParserto validate and convert the JSON into one of the two Pydantic models. - Collects all parsed events and prints them in a table.
The union type is handled automatically by LangChain's structured output parser thanks to the discriminator field.
License
MIT © 2026
Description
Languages
Python
100%