{"id":26704910,"url":"https://github.com/haseebulhassan437/structured-output-using-langchain","last_synced_at":"2026-05-07T11:34:42.142Z","repository":{"id":284588253,"uuid":"954429472","full_name":"HaseebUlHassan437/structured-output-using-LangChain","owner":"HaseebUlHassan437","description":"Demonstrates enforcing structured outputs from LLMs using LangChain (Google Gemini \u0026 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.","archived":false,"fork":false,"pushed_at":"2025-03-26T16:35:26.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T17:27:26.608Z","etag":null,"topics":["json-schema","langchain","llm","pydantic","structured-outputs","typeddict"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HaseebUlHassan437.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-25T04:21:30.000Z","updated_at":"2025-03-26T16:41:22.000Z","dependencies_parsed_at":"2025-03-26T17:27:29.948Z","dependency_job_id":"d20a982d-e8f8-4663-a88e-12d24418da81","html_url":"https://github.com/HaseebUlHassan437/structured-output-using-LangChain","commit_stats":null,"previous_names":["haseebulhassan437/structured-output-using-langchain"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HaseebUlHassan437%2Fstructured-output-using-LangChain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HaseebUlHassan437%2Fstructured-output-using-LangChain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HaseebUlHassan437%2Fstructured-output-using-LangChain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HaseebUlHassan437%2Fstructured-output-using-LangChain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HaseebUlHassan437","download_url":"https://codeload.github.com/HaseebUlHassan437/structured-output-using-LangChain/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245785805,"owners_count":20671634,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json-schema","langchain","llm","pydantic","structured-outputs","typeddict"],"created_at":"2025-03-27T05:18:34.593Z","updated_at":"2026-05-07T11:34:42.111Z","avatar_url":"https://github.com/HaseebUlHassan437.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Structured Output Demo (LangChain + Pydantic + TypedDict + Google Gemini)\n\nThis 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.\n\n---\n\n## 📁 Repository Structure\n\n| File | Description |\n|-------|-------------|\n| **pydantic_demo.py** | Shows basic Pydantic usage: defines a `Student` model, validates input data, and serializes to JSON. |\n| **json_schema.json** | A standalone JSON Schema defining a minimal “student” object (title, type, required fields). |\n| **structured_output_with_gemini.py** | Uses LangChain + HuggingFace endpoint to generate a review summary with a Pydantic schema for structured output. |\n| **structured_output_with_json_schema.py** | Uses LangChain + Google Gemini with a raw JSON Schema to enforce output structure. |\n| **typedict_test.py** | Demonstrates Python’s `TypedDict` for static‑type validated dictionaries. |\n| **with_structured_output_pydantic.py** | Uses LangChain + Google Gemini with a Pydantic model to produce strongly typed review output. |\n| **with_structured_output_typedict.py** | Uses LangChain + Google Gemini with a `TypedDict` schema for structured output and prints a specific field. |\n\n---\n\n## 🚀 Quick Start\n\n1. Clone this repo  \n   ```bash\n   git clone https://github.com/\u003cyour-username\u003e/structuredoutput.git\n   cd structuredoutput\n   ```\n\n2. Create \u0026 activate a virtual environment  \n   ```bash\n   python3 -m venv venv\n   source venv/bin/activate   # macOS/Linux\n   venv\\Scripts\\activate      # Windows\n   ```\n\n3. Install dependencies  \n   ```bash\n   pip install -r requirements.txt\n   ```\n\n4. Add your API key to a `.env` file  \n   ```\n   GOOGLE_API_KEY=your_gemini_api_key_here\n   ```\n\n5. Run any example script  \n   ```bash\n   python pydantic_demo.py\n   python structured_output_with_gemini.py\n   python with_structured_output_typedict.py\n   ```\n\n---\n\n## 🔧 Tech Stack\n\n- **LangChain** — orchestration  \n- **Google Gemini (PaLM)** via `langchain-google-genai`  \n- **Pydantic** \u0026 **TypedDict** — structured output schemas  \n- **HuggingFaceEndpoint** — alternative LLM backend  \n- **python-dotenv** — environment management  \n\n---\n\n## 📄 License\n\nMIT © 2025\n\n## 📫 Contact\n\nQuestions or feedback? Email **haseebulhassan1172003@gmail.com**.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaseebulhassan437%2Fstructured-output-using-langchain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhaseebulhassan437%2Fstructured-output-using-langchain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaseebulhassan437%2Fstructured-output-using-langchain/lists"}