https://github.com/haseebulhassan437/structured-output-using-langchain
Demonstrates enforcing structured outputs from LLMs using LangChain (Google Gemini & HuggingFace) with Pydantic, TypedDict, and JSON Schema. Includes standalone examples for data validation and schema‑driven text generation. Quickly run each script to see how to produce reliably formatted AI responses.
https://github.com/haseebulhassan437/structured-output-using-langchain
json-schema langchain llm pydantic structured-outputs typeddict
Last synced: 2 months ago
JSON representation
Demonstrates enforcing structured outputs from LLMs using LangChain (Google Gemini & HuggingFace) with Pydantic, TypedDict, and JSON Schema. Includes standalone examples for data validation and schema‑driven text generation. Quickly run each script to see how to produce reliably formatted AI responses.
- Host: GitHub
- URL: https://github.com/haseebulhassan437/structured-output-using-langchain
- Owner: HaseebUlHassan437
- License: mit
- Created: 2025-03-25T04:21:30.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2025-03-26T16:35:26.000Z (2 months ago)
- Last Synced: 2025-03-26T17:27:26.608Z (2 months ago)
- Topics: json-schema, langchain, llm, pydantic, structured-outputs, typeddict
- Language: Python
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Structured Output Demo (LangChain + Pydantic + TypedDict + Google Gemini)
This repository demonstrates multiple ways to enforce structured outputs from LLMs (LangChain + Google Gemini or HuggingFace) using Python type‑safety tools (Pydantic models, JSON Schema, and TypedDict), plus a simple Pydantic data‑validation example.
---
## 📁 Repository Structure
| File | Description |
|-------|-------------|
| **pydantic_demo.py** | Shows basic Pydantic usage: defines a `Student` model, validates input data, and serializes to JSON. |
| **json_schema.json** | A standalone JSON Schema defining a minimal “student” object (title, type, required fields). |
| **structured_output_with_gemini.py** | Uses LangChain + HuggingFace endpoint to generate a review summary with a Pydantic schema for structured output. |
| **structured_output_with_json_schema.py** | Uses LangChain + Google Gemini with a raw JSON Schema to enforce output structure. |
| **typedict_test.py** | Demonstrates Python’s `TypedDict` for static‑type validated dictionaries. |
| **with_structured_output_pydantic.py** | Uses LangChain + Google Gemini with a Pydantic model to produce strongly typed review output. |
| **with_structured_output_typedict.py** | Uses LangChain + Google Gemini with a `TypedDict` schema for structured output and prints a specific field. |---
## 🚀 Quick Start
1. Clone this repo
```bash
git clone https://github.com//structuredoutput.git
cd structuredoutput
```2. Create & activate a virtual environment
```bash
python3 -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\activate # Windows
```3. Install dependencies
```bash
pip install -r requirements.txt
```4. Add your API key to a `.env` file
```
GOOGLE_API_KEY=your_gemini_api_key_here
```5. Run any example script
```bash
python pydantic_demo.py
python structured_output_with_gemini.py
python with_structured_output_typedict.py
```---
## 🔧 Tech Stack
- **LangChain** — orchestration
- **Google Gemini (PaLM)** via `langchain-google-genai`
- **Pydantic** & **TypedDict** — structured output schemas
- **HuggingFaceEndpoint** — alternative LLM backend
- **python-dotenv** — environment management---
## 📄 License
MIT © 2025
## 📫 Contact
Questions or feedback? Email **[email protected]**.