Skip to content

Commit c949229

Browse files
axiomofjoymikeldking
authored andcommitted
refactor(annotations): use single unique constraints for identifiers (#7342)
1 parent 0378af0 commit c949229

20 files changed

+1755
-213
lines changed

packages/phoenix-client/src/phoenix/client/__generated__/v1/.dataclass.txt

+418
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/phoenix/db/insertion/evaluation.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,15 @@ async def _insert_trace_evaluation(
8686
explanation=explanation,
8787
metadata_={}, # `metadata_` must match ORM
8888
annotator_kind="LLM",
89+
identifier="",
90+
source="API",
8991
)
9092
await session.execute(
9193
insert_on_conflict(
9294
values,
9395
dialect=dialect,
9496
table=models.TraceAnnotation,
95-
unique_by=("name", "trace_rowid"),
97+
unique_by=("name", "trace_rowid", "identifier"),
9698
)
9799
)
98100
return TraceEvaluationInsertionEvent(project_rowid, evaluation_name)
@@ -128,13 +130,15 @@ async def _insert_span_evaluation(
128130
explanation=explanation,
129131
metadata_={}, # `metadata_` must match ORM
130132
annotator_kind="LLM",
133+
identifier="",
134+
source="API",
131135
)
132136
await session.execute(
133137
insert_on_conflict(
134138
values,
135139
dialect=dialect,
136140
table=models.SpanAnnotation,
137-
unique_by=("name", "span_rowid"),
141+
unique_by=("name", "span_rowid", "identifier"),
138142
)
139143
)
140144
return SpanEvaluationInsertionEvent(project_rowid, evaluation_name)
@@ -179,13 +183,21 @@ async def _insert_document_evaluation(
179183
explanation=explanation,
180184
metadata_={}, # `metadata_` must match ORM
181185
annotator_kind="LLM",
186+
identifier="",
187+
source="API",
182188
)
183189
await session.execute(
184190
insert_on_conflict(
185191
values,
186192
dialect=dialect,
187193
table=models.DocumentAnnotation,
188-
unique_by=("name", "span_rowid", "document_position"),
194+
unique_by=(
195+
"name",
196+
"span_rowid",
197+
"document_position",
198+
"identifier",
199+
),
200+
constraint_name="uq_document_annotations_name_span_rowid_document_pos_identifier", # The name of the unique constraint is specified manually since the auto-generated name is longer than the Postgres limit of 63 characters # noqa: E501
189201
)
190202
)
191203
return DocumentEvaluationInsertionEvent(project_rowid, evaluation_name)

src/phoenix/db/insertion/helpers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def insert_on_conflict(
3636
unique_by: Sequence[str],
3737
on_conflict: OnConflict = OnConflict.DO_UPDATE,
3838
set_: Optional[Mapping[str, Any]] = None,
39+
constraint_name: Optional[str] = None,
3940
) -> Insert:
4041
"""
4142
Dialect specific insertion statement using ON CONFLICT DO syntax.
@@ -50,7 +51,7 @@ def insert_on_conflict(
5051
unique_records.append(v)
5152
seen.add(k)
5253
records = tuple(reversed(unique_records))
53-
constraint = "_".join(("uq", table.__tablename__, *unique_by))
54+
constraint = constraint_name or "_".join(("uq", table.__tablename__, *unique_by))
5455
if dialect is SupportedSQLDialect.POSTGRESQL:
5556
stmt_postgresql = insert_postgresql(table).values(records)
5657
if on_conflict is OnConflict.DO_NOTHING:

0 commit comments

Comments
 (0)