Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation Error: LLM returns float instead of List[Object] in nested Pydantic models #1341

Open
psharma28 opened this issue Feb 8, 2025 · 0 comments
Labels
bug Something isn't working question Further information is requested

Comments

@psharma28
Copy link

Issue Description

When using Instructor with Anthropic/Claude model, I'm encountering validation errors where the LLM returns single float values instead of expected List[Object] structures.

Code Example

# Models
class RecipeStep(BaseModel):
    step_number: str = Field(..., description="Step number in the cooking process")
    duration: str = Field(..., description="Time required for this step")
    temperature: float = Field(..., description="Required temperature")

class CookingMethod(BaseModel):
    method_name: str = Field(..., description="Name of cooking method")
    steps: List[RecipeStep] = Field(..., description="Details of cooking steps")

class DishDetails(BaseModel):
    dish_name: str = Field(..., description="Name of the dish")
    cooking_approaches: List[CookingMethod]

class RecipeResponse(BaseModel):
    chef_explanation: str
    dish_details: List[DishDetails]

# Usage
response = anthropic_client.create(
    model="claude-3-sonnet-20240229",
    messages=[{"role": "user", "content": content}],
    response_model=RecipeResponse
)

Error Message

instructor.exceptions.InstructorRetryException: 15 validation errors for RecipeResponse
dish_details.0.cooking_approaches.0.steps
Input should be a valid array [type=list_type, input_value=350.5, input_type=float]

Expected Behavior

The LLM should return a properly structured object with steps as a List[RecipeStep] containing step objects with number, duration, and temperature.

Actual Behavior

Instead, the LLM is returning single float values where List[RecipeStep] is expected.

Environment

instructor version: 1.7.2
pydantic version: 2.10.6
Python version: 3.12
Model: Claude 3.5 V2 through AnthropicBedrock

Question

  1. Is this a known limitation with the current implementation?
  2. Are there recommended approaches to enforce the expected list structure?

Any guidance would be appreciated!

@github-actions github-actions bot added bug Something isn't working question Further information is requested labels Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant