Skip to content

Commit

Permalink
pydantic validator
Browse files Browse the repository at this point in the history
  • Loading branch information
tcapelle committed Oct 10, 2024
1 parent 125d583 commit 200c115
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions weave/flow/scorer/pydantic_scorer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from pydantic import BaseModel, ValidationError
from typing import Any, Type

from weave.flow.scorer.base_scorer import Scorer

class PydanticScorer(Scorer):
"""
Validate the model output against a pydantic model.
"""
model: Type[BaseModel]

def score(self, model_output: Any):
if isinstance(model_output, str):
try:
self.model.model_validate_json(model_output)
return True
except ValidationError:
return False
else:
try:
self.model.model_validate(model_output)
return True
except ValidationError:
return False


if __name__ == "__main__":
from pydantic import BaseModel

class User(BaseModel):
name: str
age: int

scorer = PydanticScorer(model=User)

model_output = "{\"name\": \"John\", \"age\": 30}"
print(scorer.score(model_output))

model_output = {"name": "John", "age": 30}
print(scorer.score(model_output))

0 comments on commit 200c115

Please sign in to comment.