Skip to content

Alert refactor #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/judgeval/common/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import json
from contextlib import contextmanager, asynccontextmanager, AbstractAsyncContextManager, AbstractContextManager # Import context manager bases
from dataclasses import dataclass, field
from datetime import datetime
from datetime import datetime, timezone
from http import HTTPStatus
from typing import (
Any,
Expand Down Expand Up @@ -594,7 +594,7 @@ def save(self, overwrite: bool = False) -> Tuple[str, dict]:
"trace_id": self.trace_id,
"name": self.name,
"project_name": self.project_name,
"created_at": datetime.utcfromtimestamp(self.start_time).isoformat(),
"created_at": datetime.fromtimestamp(self.start_time, timezone.utc).isoformat(),
"duration": total_duration,
"trace_spans": [span.model_dump() for span in self.trace_spans],
"evaluation_runs": [run.model_dump() for run in self.evaluation_runs],
Expand Down
42 changes: 31 additions & 11 deletions src/judgeval/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@

from judgeval.scorers import APIJudgmentScorer, JudgevalScorer

class AlertStatus(str, Enum):
"""Status of an alert evaluation."""
TRIGGERED = "triggered"
NOT_TRIGGERED = "not_triggered"
# Alert status - using boolean instead of string enum
AlertStatus = bool # True = triggered, False = not triggered

class Condition(BaseModel):
"""
Expand Down Expand Up @@ -144,7 +142,8 @@ def model_dump(self, **kwargs):
# Create standardized metric representation needed by server API
metric_data = {
"score_type": "",
"threshold": 0.0
"threshold": 0.0,
"name": ""
}

# First try to use object's own serialization methods
Expand Down Expand Up @@ -182,6 +181,16 @@ def model_dump(self, **kwargs):
# Use condition threshold if metric doesn't have one
metric_data['threshold'] = self.conditions[i].threshold

# Make sure name is set
if not metric_data.get('name'):
if hasattr(metric_obj, '__name__'):
metric_data['name'] = metric_obj.__name__
elif hasattr(metric_obj, 'name'):
metric_data['name'] = metric_obj.name
else:
# Fallback to score_type if available
metric_data['name'] = metric_data.get('score_type', str(metric_obj))

# Update the condition with our properly serialized metric
condition["metric"] = metric_data

Expand All @@ -205,7 +214,7 @@ class AlertResult(BaseModel):

Example:
{
"status": "triggered",
"status": true,
"rule_name": "Quality Check",
"conditions_result": [
{"metric": "faithfulness", "value": 0.6, "threshold": 0.7, "passed": False},
Expand All @@ -220,15 +229,19 @@ class AlertResult(BaseModel):
"enabled": true,
"communication_methods": ["slack", "email"],
"email_addresses": ["user1@example.com", "user2@example.com"]
}
},
"combined_type": "all"
}
"""
status: AlertStatus
status: bool # Changed to pure boolean
rule_id: Optional[str] = None # The unique identifier of the rule
rule_name: str
conditions_result: List[Dict[str, Any]]
metadata: Dict[str, Any] = {}
notification: Optional[NotificationConfig] = None # Configuration for notifications
notification: Optional[NotificationConfig] = None
combined_type: Optional[str] = None # Changed from types to combined_type
project_id: Optional[str] = None # Added project_id
trace_span_id: Optional[str] = None # Added trace_span_id

@property
def example_id(self) -> Optional[str]:
Expand All @@ -239,6 +252,10 @@ def example_id(self) -> Optional[str]:
def timestamp(self) -> Optional[str]:
"""Get timestamp from metadata for backward compatibility"""
return self.metadata.get("timestamp")

def model_dump(self, **kwargs):
"""Convert the AlertResult to a dictionary - status is already boolean."""
return super().model_dump(**kwargs)

class RulesEngine:
"""
Expand Down Expand Up @@ -407,7 +424,7 @@ def evaluate_rules(self, scores: Dict[str, float], example_metadata: Optional[Di
notification_config = rule.notification

# Set the alert status based on whether the rule was triggered
status = AlertStatus.TRIGGERED if triggered else AlertStatus.NOT_TRIGGERED
status = triggered # Now using boolean directly

# Create the alert result
alert_result = AlertResult(
Expand All @@ -416,7 +433,10 @@ def evaluate_rules(self, scores: Dict[str, float], example_metadata: Optional[Di
rule_name=rule.name,
conditions_result=condition_results,
notification=notification_config,
metadata=example_metadata or {}
metadata=example_metadata or {},
combined_type=rule.combine_type,
project_id=example_metadata.get("project_id") if example_metadata else None,
trace_span_id=example_metadata.get("trace_span_id") if example_metadata else None
)

results[rule_id] = alert_result
Expand Down
Loading