Skip to content

Commit

Permalink
fix: forbid blank or empty evaluation names (#2962)
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerHYang authored Apr 24, 2024
1 parent 9fdb765 commit cb87977
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/phoenix/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ class PhoenixException(Exception):

class PhoenixContextLimitExceeded(PhoenixException):
pass


class PhoenixEvaluationNameIsMissing(PhoenixException):
pass
13 changes: 12 additions & 1 deletion src/phoenix/server/api/routers/v1/evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from phoenix.config import DEFAULT_PROJECT_NAME
from phoenix.core.traces import Traces
from phoenix.db import models
from phoenix.exceptions import PhoenixEvaluationNameIsMissing
from phoenix.server.api.routers.utils import table_to_bytes
from phoenix.session.evaluation import encode_evaluations
from phoenix.trace.span_evaluations import (
Expand Down Expand Up @@ -98,6 +99,11 @@ async def post_evaluations(request: Request) -> Response:
evaluation.ParseFromString(body)
except DecodeError:
return Response("Request body is invalid", status_code=HTTP_422_UNPROCESSABLE_ENTITY)
if not evaluation.name.strip():
return Response(
"Evaluation name must not be blank/empty",
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
)
traces.put(evaluation, project_name=project_name)
return Response()

Expand Down Expand Up @@ -182,7 +188,12 @@ async def _process_pyarrow(request: Request, project_name: str, traces: Traces)
)
try:
evaluations = Evaluations.from_pyarrow_reader(reader)
except Exception:
except Exception as e:
if isinstance(e, PhoenixEvaluationNameIsMissing):
return Response(
"Evaluation name must not be blank/empty",
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
)
return Response(
content="Invalid data in request body",
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
Expand Down
7 changes: 5 additions & 2 deletions src/phoenix/trace/span_evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pyarrow import RecordBatchStreamReader, Schema, Table, parquet

from phoenix.config import TRACE_DATASET_DIR
from phoenix.exceptions import PhoenixEvaluationNameIsMissing
from phoenix.trace.errors import InvalidParquetMetadataError

EVAL_NAME_COLUMN_PREFIX = "eval."
Expand Down Expand Up @@ -335,8 +336,10 @@ def _parse_schema_metadata(schema: Schema) -> Tuple[UUID, str, Type[Evaluations]
arize_metadata = json.loads(metadata[b"arize"])
eval_classes = {subclass.__name__: subclass for subclass in Evaluations.__subclasses__()}
eval_id = UUID(arize_metadata["eval_id"])
if not isinstance((eval_name := arize_metadata["eval_name"]), str):
raise ValueError('Arize metadata must contain a string value for key "eval_name"')
if not isinstance((eval_name := arize_metadata["eval_name"]), str) or not eval_name.strip():
raise PhoenixEvaluationNameIsMissing(
'Arize metadata must contain a non-empty string value for key "eval_name"'
)
evaluations_cls = eval_classes[arize_metadata["eval_type"]]
return eval_id, eval_name, evaluations_cls
except Exception as err:
Expand Down

0 comments on commit cb87977

Please sign in to comment.