arize-phoenix 4.10.2rc1__py3-none-any.whl → 4.11.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of arize-phoenix might be problematic. Click here for more details.

@@ -1,19 +1,18 @@
1
1
  import gzip
2
2
  import zlib
3
- from typing import Any, Dict, List, Literal, Optional
3
+ from typing import Any, Dict, List
4
4
 
5
- from fastapi import APIRouter, BackgroundTasks, Header, HTTPException
6
5
  from google.protobuf.message import DecodeError
7
6
  from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import (
8
7
  ExportTraceServiceRequest,
9
8
  )
10
- from pydantic import BaseModel, Field
11
9
  from sqlalchemy import select
10
+ from starlette.background import BackgroundTask
12
11
  from starlette.concurrency import run_in_threadpool
13
12
  from starlette.datastructures import State
14
13
  from starlette.requests import Request
14
+ from starlette.responses import JSONResponse, Response
15
15
  from starlette.status import (
16
- HTTP_204_NO_CONTENT,
17
16
  HTTP_404_NOT_FOUND,
18
17
  HTTP_415_UNSUPPORTED_MEDIA_TYPE,
19
18
  HTTP_422_UNPROCESSABLE_ENTITY,
@@ -27,49 +26,40 @@ from phoenix.server.api.types.node import from_global_id_with_expected_type
27
26
  from phoenix.trace.otel import decode_otlp_span
28
27
  from phoenix.utilities.project import get_project_name
29
28
 
30
- from .utils import RequestBody, ResponseBody, add_errors_to_responses
31
-
32
- router = APIRouter(tags=["traces"], include_in_schema=False)
33
-
34
-
35
- @router.post(
36
- "/traces",
37
- operation_id="addTraces",
38
- summary="Send traces",
39
- status_code=HTTP_204_NO_CONTENT,
40
- responses=add_errors_to_responses(
41
- [
42
- {
43
- "status_code": HTTP_415_UNSUPPORTED_MEDIA_TYPE,
44
- "description": (
45
- "Unsupported content type (only `application/x-protobuf` is supported)"
46
- ),
47
- },
48
- {"status_code": HTTP_422_UNPROCESSABLE_ENTITY, "description": "Invalid request body"},
49
- ]
50
- ),
51
- openapi_extra={
52
- "requestBody": {
53
- "required": True,
54
- "content": {
55
- "application/x-protobuf": {"schema": {"type": "string", "format": "binary"}}
56
- },
57
- }
58
- },
59
- )
60
- async def post_traces(
61
- request: Request,
62
- content_type: Optional[str] = Header(default=None),
63
- content_encoding: Optional[str] = Header(default=None),
64
- ) -> None:
29
+
30
+ async def post_traces(request: Request) -> Response:
31
+ """
32
+ summary: Send traces to Phoenix
33
+ operationId: addTraces
34
+ tags:
35
+ - private
36
+ requestBody:
37
+ required: true
38
+ content:
39
+ application/x-protobuf:
40
+ schema:
41
+ type: string
42
+ format: binary
43
+ responses:
44
+ 200:
45
+ description: Success
46
+ 403:
47
+ description: Forbidden
48
+ 415:
49
+ description: Unsupported content type, only gzipped protobuf
50
+ 422:
51
+ description: Request body is invalid
52
+ """
53
+ content_type = request.headers.get("content-type")
65
54
  if content_type != "application/x-protobuf":
66
- raise HTTPException(
67
- detail=f"Unsupported content type: {content_type}",
55
+ return Response(
56
+ content=f"Unsupported content type: {content_type}",
68
57
  status_code=HTTP_415_UNSUPPORTED_MEDIA_TYPE,
69
58
  )
59
+ content_encoding = request.headers.get("content-encoding")
70
60
  if content_encoding and content_encoding not in ("gzip", "deflate"):
71
- raise HTTPException(
72
- detail=f"Unsupported content encoding: {content_encoding}",
61
+ return Response(
62
+ content=f"Unsupported content encoding: {content_encoding}",
73
63
  status_code=HTTP_415_UNSUPPORTED_MEDIA_TYPE,
74
64
  )
75
65
  body = await request.body()
@@ -81,69 +71,96 @@ async def post_traces(
81
71
  try:
82
72
  await run_in_threadpool(req.ParseFromString, body)
83
73
  except DecodeError:
84
- raise HTTPException(
85
- detail="Request body is invalid ExportTraceServiceRequest",
74
+ return Response(
75
+ content="Request body is invalid ExportTraceServiceRequest",
86
76
  status_code=HTTP_422_UNPROCESSABLE_ENTITY,
87
77
  )
88
- BackgroundTasks().add_task(_add_spans, req, request.state)
89
- return None
90
-
91
-
92
- class AnnotationResult(BaseModel):
93
- label: Optional[str] = Field(default=None, description="The label assigned by the annotation")
94
- score: Optional[float] = Field(default=None, description="The score assigned by the annotation")
95
- explanation: Optional[str] = Field(
96
- default=None, description="Explanation of the annotation result"
97
- )
98
-
99
-
100
- class TraceAnnotation(BaseModel):
101
- trace_id: str = Field(description="The ID of the trace being annotated")
102
- name: str = Field(description="The name of the annotation")
103
- annotator_kind: Literal["LLM", "HUMAN"] = Field(
104
- description="The kind of annotator used for the annotation"
105
- )
106
- result: Optional[AnnotationResult] = Field(
107
- default=None, description="The result of the annotation"
108
- )
109
- metadata: Optional[Dict[str, Any]] = Field(
110
- default=None, description="Metadata for the annotation"
111
- )
112
-
113
-
114
- class AnnotateTracesRequestBody(RequestBody[List[TraceAnnotation]]):
115
- data: List[TraceAnnotation] = Field(description="The trace annotations to be upserted")
116
-
117
-
118
- class InsertedTraceAnnotation(BaseModel):
119
- id: str = Field(description="The ID of the inserted trace annotation")
120
-
121
-
122
- class AnnotateTracesResponseBody(ResponseBody[List[InsertedTraceAnnotation]]):
123
- pass
124
-
125
-
126
- @router.post(
127
- "/trace_annotations",
128
- operation_id="annotateTraces",
129
- summary="Create or update trace annotations",
130
- responses=add_errors_to_responses(
131
- [{"status_code": HTTP_404_NOT_FOUND, "description": "Trace not found"}]
132
- ),
133
- )
134
- async def annotate_traces(
135
- request: Request, request_body: AnnotateTracesRequestBody
136
- ) -> AnnotateTracesResponseBody:
137
- trace_annotations = request_body.data
138
- trace_gids = [GlobalID.from_id(annotation.trace_id) for annotation in trace_annotations]
78
+ return Response(background=BackgroundTask(_add_spans, req, request.state))
79
+
80
+
81
+ async def annotate_traces(request: Request) -> Response:
82
+ """
83
+ summary: Upsert annotations for traces
84
+ operationId: annotateTraces
85
+ tags:
86
+ - private
87
+ requestBody:
88
+ description: List of trace annotations to be inserted
89
+ required: true
90
+ content:
91
+ application/json:
92
+ schema:
93
+ type: object
94
+ properties:
95
+ data:
96
+ type: array
97
+ items:
98
+ type: object
99
+ properties:
100
+ trace_id:
101
+ type: string
102
+ description: The ID of the trace being annotated
103
+ name:
104
+ type: string
105
+ description: The name of the annotation
106
+ annotator_kind:
107
+ type: string
108
+ description: The kind of annotator used for the annotation ("LLM" or "HUMAN")
109
+ result:
110
+ type: object
111
+ description: The result of the annotation
112
+ properties:
113
+ label:
114
+ type: string
115
+ description: The label assigned by the annotation
116
+ score:
117
+ type: number
118
+ format: float
119
+ description: The score assigned by the annotation
120
+ explanation:
121
+ type: string
122
+ description: Explanation of the annotation result
123
+ error:
124
+ type: string
125
+ description: Optional error message if the annotation encountered an error
126
+ metadata:
127
+ type: object
128
+ description: Metadata for the annotation
129
+ additionalProperties:
130
+ type: string
131
+ required:
132
+ - trace_id
133
+ - name
134
+ - annotator_kind
135
+ responses:
136
+ 200:
137
+ description: Trace annotations inserted successfully
138
+ content:
139
+ application/json:
140
+ schema:
141
+ type: object
142
+ properties:
143
+ data:
144
+ type: array
145
+ items:
146
+ type: object
147
+ properties:
148
+ id:
149
+ type: string
150
+ description: The ID of the inserted trace annotation
151
+ 404:
152
+ description: Trace not found
153
+ """
154
+ payload: List[Dict[str, Any]] = (await request.json()).get("data", [])
155
+ trace_gids = [GlobalID.from_id(annotation["trace_id"]) for annotation in payload]
139
156
 
140
157
  resolved_trace_ids = []
141
158
  for trace_gid in trace_gids:
142
159
  try:
143
160
  resolved_trace_ids.append(from_global_id_with_expected_type(trace_gid, "Trace"))
144
161
  except ValueError:
145
- raise HTTPException(
146
- detail="Trace with ID {trace_gid} does not exist",
162
+ return Response(
163
+ content="Trace with ID {trace_gid} does not exist",
147
164
  status_code=HTTP_404_NOT_FOUND,
148
165
  )
149
166
 
@@ -158,24 +175,24 @@ async def annotate_traces(
158
175
  missing_trace_gids = [
159
176
  str(GlobalID("Trace", str(trace_gid))) for trace_gid in missing_trace_ids
160
177
  ]
161
- raise HTTPException(
162
- detail=f"Traces with IDs {', '.join(missing_trace_gids)} do not exist.",
178
+ return Response(
179
+ content=f"Traces with IDs {', '.join(missing_trace_gids)} do not exist.",
163
180
  status_code=HTTP_404_NOT_FOUND,
164
181
  )
165
182
 
166
183
  inserted_annotations = []
167
184
 
168
- for annotation in trace_annotations:
169
- trace_gid = GlobalID.from_id(annotation.trace_id)
185
+ for annotation in payload:
186
+ trace_gid = GlobalID.from_id(annotation["trace_id"])
170
187
  trace_id = from_global_id_with_expected_type(trace_gid, "Trace")
171
188
 
172
- name = annotation.name
173
- annotator_kind = annotation.annotator_kind
174
- result = annotation.result
175
- label = result.label if result else None
176
- score = result.score if result else None
177
- explanation = result.explanation if result else None
178
- metadata = annotation.metadata or {}
189
+ name = annotation["name"]
190
+ annotator_kind = annotation["annotator_kind"]
191
+ result = annotation.get("result")
192
+ label = result.get("label") if result else None
193
+ score = result.get("score") if result else None
194
+ explanation = result.get("explanation") if result else None
195
+ metadata = annotation.get("metadata") or {}
179
196
 
180
197
  values = dict(
181
198
  trace_rowid=trace_id,
@@ -196,12 +213,10 @@ async def annotate_traces(
196
213
  ).returning(models.TraceAnnotation.id)
197
214
  )
198
215
  inserted_annotations.append(
199
- InsertedTraceAnnotation(
200
- id=str(GlobalID("TraceAnnotation", str(trace_annotation_id)))
201
- )
216
+ {"id": str(GlobalID("TraceAnnotation", str(trace_annotation_id)))}
202
217
  )
203
218
 
204
- return AnnotateTracesResponseBody(data=inserted_annotations)
219
+ return JSONResponse(content={"data": inserted_annotations})
205
220
 
206
221
 
207
222
  async def _add_spans(req: ExportTraceServiceRequest, state: State) -> None:
@@ -62,6 +62,7 @@ class SpanKind(Enum):
62
62
  agent = "AGENT"
63
63
  reranker = "RERANKER"
64
64
  evaluator = "EVALUATOR"
65
+ guardrail = "GUARDRAIL"
65
66
  unknown = "UNKNOWN"
66
67
 
67
68
  @classmethod