forked from Puneethnitc/Cooking-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpydanticLayer.py
More file actions
36 lines (25 loc) · 730 Bytes
/
pydanticLayer.py
File metadata and controls
36 lines (25 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import json
from pydantic import BaseModel, ValidationError
from typing import List, Optional
class Ingredient(BaseModel):
name: str
quantity: str
substitute: Optional[str] = None
class Step(BaseModel):
step_number: int
instruction: str
class Recipe(BaseModel):
dish_name: str
ingredients: List[Ingredient]
steps: List[Step]
cooking_time: str
difficulty: str
def validate_recipe(raw_output: str):
try:
data = json.loads(raw_output)
recipe = Recipe(**data)
return recipe, None
except json.JSONDecodeError as e:
return None, f"JSONDecodeError: {str(e)}"
except ValidationError as e:
return None, f"ValidationError: {str(e)}"