File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 } " )
You can’t perform that action at this time.
0 commit comments