Skip to content

fix(annotations): ensure user identifiers are stable #7414

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 3 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 8 additions & 14 deletions src/phoenix/server/api/mutations/span_annotations_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from sqlalchemy import delete, insert, select
from starlette.requests import Request
from strawberry import UNSET, Info
from strawberry.relay import GlobalID

from phoenix.db import models
from phoenix.server.api.auth import IsLocked, IsNotReadOnly
Expand Down Expand Up @@ -62,16 +63,13 @@ async def create_span_annotations(

async with info.context.db() as session:
for idx, (span_rowid, annotation_input) in enumerate(zip(span_rowids, input)):
resolved_identifier = annotation_input.identifier or ""
if annotation_input.source == AnnotationSource.APP:
resolved_identifier = ""
if annotation_input.identifier:
resolved_identifier = annotation_input.identifier
elif annotation_input.source == AnnotationSource.APP and user_id is not None:
# Ensure that the annotation has a per-user identifier if submitted via the UI
if user_id is not None:
username = await session.scalar(
select(models.User.username).where(models.User.id == user_id)
)
resolved_identifier = f"px-app:{username}"
else:
resolved_identifier = "px-app"
user_gid = str(GlobalID(type_name="User", node_id=str(user_id)))
resolved_identifier = f"px-app:{user_gid}"
values = {
"span_rowid": span_rowid,
"name": annotation_input.name,
Expand All @@ -90,12 +88,8 @@ async def create_span_annotations(
q = select(models.SpanAnnotation).where(
models.SpanAnnotation.span_rowid == span_rowid,
models.SpanAnnotation.name == annotation_input.name,
models.SpanAnnotation.identifier == resolved_identifier,
)
if resolved_identifier is None:
q = q.where(models.SpanAnnotation.identifier.is_(None))
else:
q = q.where(models.SpanAnnotation.identifier == resolved_identifier)

existing_annotation = await session.scalar(q)

if existing_annotation:
Expand Down
22 changes: 8 additions & 14 deletions src/phoenix/server/api/mutations/trace_annotations_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sqlalchemy import delete, insert, select
from starlette.requests import Request
from strawberry import UNSET, Info
from strawberry.relay.types import GlobalID

from phoenix.db import models
from phoenix.server.api.auth import IsLocked, IsNotReadOnly
Expand Down Expand Up @@ -55,16 +56,13 @@ async def create_trace_annotations(

async with info.context.db() as session:
for idx, (trace_rowid, annotation_input) in enumerate(zip(trace_rowids, input)):
resolved_identifier = annotation_input.identifier or ""
if annotation_input.source == AnnotationSource.APP:
resolved_identifier = ""
if annotation_input.identifier:
resolved_identifier = annotation_input.identifier
elif annotation_input.source == AnnotationSource.APP and user_id is not None:
# Ensure that the annotation has a per-user identifier if submitted via the UI
if user_id is not None:
username = await session.scalar(
select(models.User.username).where(models.User.id == user_id)
)
resolved_identifier = f"px-app:{username}"
else:
resolved_identifier = "px-app"
user_gid = str(GlobalID(type_name="User", node_id=str(user_id)))
resolved_identifier = f"px-app:{user_gid}"
values = {
"trace_rowid": trace_rowid,
"name": annotation_input.name,
Expand All @@ -84,12 +82,8 @@ async def create_trace_annotations(
q = select(models.TraceAnnotation).where(
models.TraceAnnotation.trace_rowid == trace_rowid,
models.TraceAnnotation.name == annotation_input.name,
models.TraceAnnotation.identifier == resolved_identifier,
)
if resolved_identifier is None:
q = q.where(models.TraceAnnotation.identifier.is_(None))
else:
q = q.where(models.TraceAnnotation.identifier == resolved_identifier)

existing_annotation = await session.scalar(q)

if existing_annotation:
Expand Down
Loading