{"id":30910227,"url":"https://github.com/jsonusuman351/langchain_structured_output","last_synced_at":"2026-05-02T17:35:02.987Z","repository":{"id":313259302,"uuid":"1050648213","full_name":"jsonusuman351/Langchain_Structured_Output","owner":"jsonusuman351","description":"A practical guide to getting reliable, structured JSON output from LLMs using LangChain, demonstrating schema enforcement with Pydantic, TypedDict, and JSON Schema.","archived":false,"fork":false,"pushed_at":"2025-09-04T20:30:14.000Z","size":8,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-04T22:23:06.912Z","etag":null,"topics":["json-schema","langchain","openai","pydantic","python","structured-output"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsonusuman351.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-09-04T18:18:23.000Z","updated_at":"2025-09-04T20:35:11.000Z","dependencies_parsed_at":"2025-09-04T22:23:08.624Z","dependency_job_id":"a6daf777-8f19-4fc8-b03d-1bde26098e21","html_url":"https://github.com/jsonusuman351/Langchain_Structured_Output","commit_stats":null,"previous_names":["jsonusuman351/langchain_structured_output"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/jsonusuman351/Langchain_Structured_Output","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonusuman351%2FLangchain_Structured_Output","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonusuman351%2FLangchain_Structured_Output/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonusuman351%2FLangchain_Structured_Output/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonusuman351%2FLangchain_Structured_Output/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsonusuman351","download_url":"https://codeload.github.com/jsonusuman351/Langchain_Structured_Output/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonusuman351%2FLangchain_Structured_Output/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279005914,"owners_count":26083995,"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","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","openai","pydantic","python","structured-output"],"created_at":"2025-09-09T17:03:03.742Z","updated_at":"2025-10-11T02:07:01.961Z","avatar_url":"https://github.com/jsonusuman351.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ⚙️ Mastering Structured Output with LangChain\n\n![Python](https://img.shields.io/badge/Python-3.10-blue?style=for-the-badge\u0026logo=python) ![LangChain](https://img.shields.io/badge/LangChain-0086CB?style=for-the-badge\u0026logo=langchain) ![OpenAI](https://img.shields.io/badge/OpenAI-412991?style=for-the-badge\u0026logo=openai) ![Pydantic](https://img.shields.io/badge/Pydantic-E92063?style=for-the-badge\u0026logo=pydantic)\n\nWelcome to this hands-on guide for taming the output of Large Language Models! By default, LLMs produce unstructured text, which is great for conversation but difficult to use in downstream systems like databases, APIs, or AI agents. This repository explores how to force LLMs to return clean, predictable, and structured data (like JSON) using LangChain.\n\nThis collection of scripts is a deep dive into the `with_structured_output` method, showcasing how to define a desired data schema using powerful Python libraries.\n\n---\n\n### 🤔 Why Do We Need Structured Output?\n\nLLMs are creative, but software systems are not. To make AI applications reliable, we need predictable data formats. Structured output is essential for:\n\n-   **Data Extraction**: Reliably pulling specific information from a block of text (e.g., extracting user details from a query).\n-   **API Building**: Ensuring the LLM's response can be directly used to call another API or service without messy parsing.\n-   **AI Agents \u0026 Function Calling**: Providing tools and functions to an AI agent in a format it can understand and execute.\n-   **Database Integration**: Formatting LLM output so it can be directly inserted into a database table.\n\n---\n\n### ✨ Core Concepts Demonstrated\n\nThis repository explores the three primary methods for defining a data schema with LangChain's `with_structured_output` method:\n\n1.  **TypedDict**:\n    -   A simple and direct way to define a dictionary's structure with type hints.\n    -   Ideal for straightforward cases where you just need a dictionary with specific keys and value types. The output is a standard Python `dict`.\n\n2.  **Pydantic**:\n    -   The most powerful and recommended approach. Pydantic models not only define the structure but also perform **data validation**, coercion, and can handle default values and optional fields.\n    -   It returns a Pydantic model **object**, which allows for cleaner code and attribute access (e.g., `result.name` instead of `result['name']`).\n\n3.  **JSON Schema**:\n    -   A language-agnostic standard for defining the structure of JSON data.\n    -   This method is perfect for multi-language environments or when the required data schema is already defined in JSON Schema format. The output is a Python `dict`.\n\nThis collection of scripts also explores **JSON Mode and Function Calling**, the underlying mechanisms that models like OpenAI, Gemini, and Claude use to produce structured data.\n\n---\n\n### 🛠️ Tech Stack\n\n-   **Core Framework**: LangChain\n-   **LLM Provider**: OpenAI\n-   **Data Validation \u0026 Schemas**: Pydantic, TypedDict\n-   **Core Libraries**: `langchain-core`, `langchain-openai`, `python-dotenv`\n\n---\n\n### ⚙️ Setup and Installation\n\n1.  **Clone the repository:**\n    ```bash\n    git clone [https://github.com/jsonusuman351/Langchain_Structured_Output.git](https://github.com/jsonusuman351/Langchain_Structured_Output.git)\n    cd Langchain_Structured_Output\n    ```\n\n2.  **Create and activate a virtual environment:**\n    ```bash\n    # It is recommended to use Python 3.10 or higher\n    python -m venv venv\n    .\\venv\\Scripts\\activate\n    ```\n\n3.  **Install the required packages:**\n    ```bash\n    pip install -r requirements.txt\n    ```\n\n4.  **Set Up Environment Variables:**\n    -   Create a file named `.env` in the root directory.\n    -   Add your OpenAI API key to this file:\n        ```env\n        OPENAI_API_KEY=\"your-openai-api-key\"\n        ```\n\n---\n\n### 🚀 Usage Guide\n\nEach script in this repository demonstrates how to extract structured information from a sample sentence using a different schema definition method.\n\n-   **Using Pydantic for Validation and Object Output:**\n    *This is the most robust method.*\n    ```bash\n    python with_structured_output_pydantic.py\n    ```\n\n-   **Using TypedDict for Simple Dictionary Output:**\n    *A lightweight and straightforward approach.*\n    ```bash\n    python with_structured_output_typeddict.py\n    ```\n\n-   **Using JSON Schema for Language-Agnostic Definitions:**\n    *Ideal for cross-platform compatibility.*\n    ```bash\n    python with_structured_output_json.py\n    ```\n\n**Example Output (from `with_structured_output_pydantic.py`):**\n```\nPerson(name='Suman', age=24)\n\u003cclass '__main__.Person'\u003e\n```\n\n---\n\n### 🔬 A Tour of the Structuring Methods\n\nThis repository is organized by the schema definition technique, allowing you to compare each approach directly.\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to view the code layout\u003c/summary\u003e\n\n```\nLangchain_Structured_Output/\n│\n├── with_structured_output_pydantic.py  # Recommended: Uses Pydantic for robust validation\n├── with_structured_output_typeddict.py # Simple: Uses Python's built-in TypedDict\n├── with_structured_output_json.py    # Flexible: Uses a standard JSON Schema file\n│\n├── Pydantic.py                         # Defines the Pydantic model\n├── typeddict.py                        # Defines the TypedDict model\n├── json_schema.json                    # Contains the JSON Schema definition\n│\n├── requirements.txt\n├── .env                                # (Need to create this for your API key)\n└── README.md\n```\n\u003c/details\u003e\n\n---\n\n---","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsonusuman351%2Flangchain_structured_output","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsonusuman351%2Flangchain_structured_output","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsonusuman351%2Flangchain_structured_output/lists"}