[GH-ISSUE #40] Add Pydantic model support to LLM schema handler #8

Closed
opened 2026-03-02 04:07:47 +03:00 by kerem · 1 comment
Owner

Originally created by @gadievron on GitHub (Dec 22, 2025).
Original GitHub issue: https://github.com/gadievron/raptor/issues/40

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 for flexibility, but current implementation doesn't detect when Pydantic model is passed.

Impact

  • TypeError when Instructor passes Pydantic models
  • Breaks LLM structured output generation
  • Limits flexibility of hybrid approach

Fix

Add Pydantic model detection:

from inspect import isclass

def _dict_schema_to_pydantic(schema: Union[Dict[str, Any], Type[BaseModel]]) -> Type[BaseModel]:
    if isclass(schema) and issubclass(schema, BaseModel):
        return schema
    if not isinstance(schema, dict):
        raise ValueError(f"Schema must be dict or Pydantic BaseModel class")
    # ... rest of dict processing ...

File: packages/llm_analysis/llm/providers.py:88-95

Also requires type annotation updates across codebase.

Type

  • Bug fix + Enhancement
  • Backward compatible
  • Bug #2 from internal analysis
  • Supports RAPTOR's "flexibility over strictness" philosophy
Originally created by @gadievron on GitHub (Dec 22, 2025). Original GitHub issue: https://github.com/gadievron/raptor/issues/40 ## 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 for flexibility, but current implementation doesn't detect when Pydantic model is passed. ## Impact - TypeError when Instructor passes Pydantic models - Breaks LLM structured output generation - Limits flexibility of hybrid approach ## Fix Add Pydantic model detection: ```python from inspect import isclass def _dict_schema_to_pydantic(schema: Union[Dict[str, Any], Type[BaseModel]]) -> Type[BaseModel]: if isclass(schema) and issubclass(schema, BaseModel): return schema if not isinstance(schema, dict): raise ValueError(f"Schema must be dict or Pydantic BaseModel class") # ... rest of dict processing ... ``` **File:** `packages/llm_analysis/llm/providers.py:88-95` Also requires type annotation updates across codebase. ## Type - Bug fix + Enhancement - Backward compatible ## Related - Bug #2 from internal analysis - Supports RAPTOR's "flexibility over strictness" philosophy
kerem 2026-03-02 04:07:47 +03:00
Author
Owner

@gadievron commented on GitHub (Dec 22, 2025):

Fixed in PR #46

<!-- gh-comment-id:3684216951 --> @gadievron commented on GitHub (Dec 22, 2025): Fixed in PR #46
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#8
No description provided.