Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions examples/task_retries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Example showing how to control retries for Tasks with pydantic output."""

from pydantic import BaseModel, Field, field_validator

import marvin


class SpecialCode(BaseModel):
"""A model with a custom validation rule that requires learning from errors."""

code: str = Field(description="The special code")

@field_validator("code")

Copilot AI Jul 17, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field validator is missing a classmethod decorator. It should be @classmethod to properly define a class method that receives cls as the first parameter.

Suggested change
@field_validator("code")
@field_validator("code")
@classmethod

Copilot uses AI. Check for mistakes.
def validate_code(cls, v):
# This validator has a special requirement: the code must end with "-42X"
# The agent won't know this initially and will learn from the error
if not v.endswith("-42X"):
raise ValueError("Code must end with '-42X' (company policy)")
return v


# Set custom retry limit (default is 10)
marvin.settings.agent_retries = 3

# Use a thread to maintain conversation history across retries
thread = marvin.Thread()

# Create a task that will likely fail on first attempt
task = marvin.Task[SpecialCode](
instructions="Generate a special code for project ALPHA",
result_type=SpecialCode,
)

print(f"Running task with retry limit of {marvin.settings.agent_retries}...")
result = task.run(thread=thread)
print(f"Successfully generated: {result}")