[PR #46] [MERGED] Add Pydantic model support to LLM schema handler #51

Closed
opened 2026-03-02 04:08:02 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/gadievron/raptor/pull/46
Author: @gadievron
Created: 12/22/2025
Status: Merged
Merged: 12/26/2025
Merged by: @danielcuthbert

Base: mainHead: fix/bug-40-pydantic-schema


📝 Commits (1)

  • a88cf15 Add Pydantic model support to LLM schema handler

📊 Changes

1 file changed (+21 additions, -5 deletions)

View changed files

📝 packages/llm_analysis/llm/providers.py (+21 -5)

📄 Description

Summary

Extends _dict_schema_to_pydantic() to accept both dict schemas and Pydantic model classes, supporting RAPTOR's intentional hybrid approach for flexibility.

Problem

The function _dict_schema_to_pydantic() in packages/llm_analysis/llm/providers.py only handles dict schemas, but RAPTOR's hybrid design allows both dicts and Pydantic models. When Instructor library passes Pydantic models directly, TypeError occurs, breaking LLM structured output generation.

Root Cause

RAPTOR intentionally uses hybrid Pydantic/dict approach:

  • Dicts for flexibility and dynamic schemas
  • Pydantic for type safety and validation
  • Current implementation doesn't detect when Pydantic model is passed

Changes

1. Add Pydantic Model Detection

File: packages/llm_analysis/llm/providers.py
Lines: 65-101

  • Added Union[Dict[str, Any], Type[BaseModel]] type support
  • Added isclass() check to detect Pydantic models
  • Return Pydantic models as-is when already provided
  • Added validation for invalid schema types

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves existing functionality)

Impact

  • Risk: Low - Adds capability without removing existing functionality
  • Scope: LLM provider schema handling
  • Breaking: No - Backward compatible with existing dict usage
  • Performance: Negligible - One additional type check

Fixes #40


Note

Adds Pydantic model class support to LLM schema handling and validates input types.

  • _dict_schema_to_pydantic now accepts either dict schemas or Pydantic BaseModel classes; returns models as-is, converts dicts to dynamic models
  • Adds isclass check, updates type hints to Union[Dict[str, Any], Type[BaseModel]], and raises ValueError for unsupported types
  • Minor import/type updates in providers.py; no other provider logic changed

Written by Cursor Bugbot for commit a88cf150f7. This will update automatically on new commits. Configure here.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/gadievron/raptor/pull/46 **Author:** [@gadievron](https://github.com/gadievron) **Created:** 12/22/2025 **Status:** ✅ Merged **Merged:** 12/26/2025 **Merged by:** [@danielcuthbert](https://github.com/danielcuthbert) **Base:** `main` ← **Head:** `fix/bug-40-pydantic-schema` --- ### 📝 Commits (1) - [`a88cf15`](https://github.com/gadievron/raptor/commit/a88cf150f728bac95dfb58bd3c08cae6b5071cde) Add Pydantic model support to LLM schema handler ### 📊 Changes **1 file changed** (+21 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `packages/llm_analysis/llm/providers.py` (+21 -5) </details> ### 📄 Description ## Summary Extends `_dict_schema_to_pydantic()` to accept both dict schemas and Pydantic model classes, supporting RAPTOR's intentional hybrid approach for flexibility. ## Problem The function `_dict_schema_to_pydantic()` in `packages/llm_analysis/llm/providers.py` only handles dict schemas, but RAPTOR's hybrid design allows both dicts and Pydantic models. When Instructor library passes Pydantic models directly, TypeError occurs, breaking LLM structured output generation. ## Root Cause RAPTOR intentionally uses hybrid Pydantic/dict approach: - Dicts for flexibility and dynamic schemas - Pydantic for type safety and validation - Current implementation doesn't detect when Pydantic model is passed ## Changes ### 1. Add Pydantic Model Detection **File:** `packages/llm_analysis/llm/providers.py` **Lines:** 65-101 - Added `Union[Dict[str, Any], Type[BaseModel]]` type support - Added `isclass()` check to detect Pydantic models - Return Pydantic models as-is when already provided - Added validation for invalid schema types ## Type of Change - [x] Bug fix (non-breaking change which fixes an issue) - [x] Enhancement (improves existing functionality) ## Impact - **Risk:** Low - Adds capability without removing existing functionality - **Scope:** LLM provider schema handling - **Breaking:** No - Backward compatible with existing dict usage - **Performance:** Negligible - One additional type check Fixes #40 <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds Pydantic model class support to `LLM` schema handling and validates input types. > > - **`_dict_schema_to_pydantic`** now accepts either dict schemas or Pydantic `BaseModel` classes; returns models as-is, converts dicts to dynamic models > - Adds `isclass` check, updates type hints to `Union[Dict[str, Any], Type[BaseModel]]`, and raises `ValueError` for unsupported types > - Minor import/type updates in `providers.py`; no other provider logic changed > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit a88cf150f728bac95dfb58bd3c08cae6b5071cde. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-02 04:08:02 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/raptor#51
No description provided.