Skip to content

Commit 577b55d

Browse files
authored
add retry example (#1179)
1 parent 1034eef commit 577b55d

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

examples/task_retries.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Example showing how to control retries for Tasks with pydantic output."""
2+
3+
from pydantic import BaseModel, Field, field_validator
4+
5+
import marvin
6+
7+
8+
class SpecialCode(BaseModel):
9+
"""A model with a custom validation rule that requires learning from errors."""
10+
11+
code: str = Field(description="The special code")
12+
13+
@field_validator("code")
14+
def validate_code(cls, v):
15+
# This validator has a special requirement: the code must end with "-42X"
16+
# The agent won't know this initially and will learn from the error
17+
if not v.endswith("-42X"):
18+
raise ValueError("Code must end with '-42X' (company policy)")
19+
return v
20+
21+
22+
# Set custom retry limit (default is 10)
23+
marvin.settings.agent_retries = 3
24+
25+
# Use a thread to maintain conversation history across retries
26+
thread = marvin.Thread()
27+
28+
# Create a task that will likely fail on first attempt
29+
task = marvin.Task[SpecialCode](
30+
instructions="Generate a special code for project ALPHA",
31+
result_type=SpecialCode,
32+
)
33+
34+
print(f"Running task with retry limit of {marvin.settings.agent_retries}...")
35+
result = task.run(thread=thread)
36+
print(f"Successfully generated: {result}")

0 commit comments

Comments
 (0)