Skip to content

Commit 740e8bd

Browse files
authored
Revert "fix: tracer leak and cost tracking guards (#15084)" (#15317)
This reverts commit 70c1225.
1 parent 840f6cb commit 740e8bd

10 files changed

Lines changed: 526 additions & 1493 deletions

File tree

src/phoenix/server/api/routers/agents.py

Lines changed: 202 additions & 250 deletions
Large diffs are not rendered by default.

src/phoenix/server/api/subscriptions.py

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -84,53 +84,53 @@ async def _stream_single_chat_completion(
8484
span_cost_calculator: SpanCostCalculator,
8585
otel_context: OtelContext,
8686
) -> ChatStream:
87-
async with Tracer(span_cost_calculator=span_cost_calculator) as tracer:
88-
try:
89-
messages = prompt_chat_template_to_playground_messages(
90-
input.prompt_version.template.to_orm()
91-
)
92-
if template_options := input.template:
93-
messages = formatted_messages(
94-
messages=messages,
95-
template_format=template_options.format,
96-
template_variables=cast(Mapping[str, Any], template_options.variables),
97-
)
98-
invocation_parameters = input.prompt_version.invocation_parameters.to_orm()
99-
tools = input.prompt_version.tools.to_orm() if input.prompt_version.tools else None
100-
response_format = (
101-
input.prompt_version.response_format.to_orm()
102-
if input.prompt_version.response_format
103-
else None
104-
)
105-
106-
async for chunk in llm_client.chat_completion_create(
87+
tracer = Tracer(span_cost_calculator=span_cost_calculator)
88+
try:
89+
messages = prompt_chat_template_to_playground_messages(
90+
input.prompt_version.template.to_orm()
91+
)
92+
if template_options := input.template:
93+
messages = formatted_messages(
10794
messages=messages,
108-
tools=tools,
109-
response_format=response_format,
110-
invocation_parameters=invocation_parameters,
111-
tracer=tracer,
112-
otel_context=otel_context,
113-
stream_model_output=input.stream_model_output,
114-
):
115-
chunk.repetition_number = repetition_number
116-
yield chunk
117-
except Exception as error:
118-
yield ChatCompletionSubscriptionError(
119-
message=str(error),
120-
repetition_number=repetition_number,
95+
template_format=template_options.format,
96+
template_variables=cast(Mapping[str, Any], template_options.variables),
12197
)
98+
invocation_parameters = input.prompt_version.invocation_parameters.to_orm()
99+
tools = input.prompt_version.tools.to_orm() if input.prompt_version.tools else None
100+
response_format = (
101+
input.prompt_version.response_format.to_orm()
102+
if input.prompt_version.response_format
103+
else None
104+
)
122105

123-
db_traces = tracer.get_db_traces(project_id=project_id)
124-
async with db() as session:
125-
session.add_all(db_traces)
126-
await session.flush()
127-
if db_traces and db_traces[0].spans:
128-
db_span = db_traces[0].spans[0]
129-
yield ChatCompletionSubscriptionResult(
130-
span=Span(id=db_span.id, db_record=db_span),
131-
repetition_number=repetition_number,
132-
)
133-
on_span_insertion()
106+
async for chunk in llm_client.chat_completion_create(
107+
messages=messages,
108+
tools=tools,
109+
response_format=response_format,
110+
invocation_parameters=invocation_parameters,
111+
tracer=tracer,
112+
otel_context=otel_context,
113+
stream_model_output=input.stream_model_output,
114+
):
115+
chunk.repetition_number = repetition_number
116+
yield chunk
117+
except Exception as error:
118+
yield ChatCompletionSubscriptionError(
119+
message=str(error),
120+
repetition_number=repetition_number,
121+
)
122+
123+
db_traces = tracer.get_db_traces(project_id=project_id)
124+
async with db() as session:
125+
session.add_all(db_traces)
126+
await session.flush()
127+
if db_traces and db_traces[0].spans:
128+
db_span = db_traces[0].spans[0]
129+
yield ChatCompletionSubscriptionResult(
130+
span=Span(id=db_span.id, db_record=db_span),
131+
repetition_number=repetition_number,
132+
)
133+
on_span_insertion()
134134

135135

136136
async def _cleanup_chat_completion_resources(

src/phoenix/server/cost_tracking/token_cost_calculator.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,7 @@ def calculate_cost(
3434
attributes: Mapping[str, Any],
3535
tokens: float,
3636
) -> float:
37-
v = get_attribute_value(attributes, self.key)
38-
# OTLP preserves the type the client sent, so this attribute can be a
39-
# string or a list. Comparing one against the threshold raises, and span
40-
# ingestion catches per span, so the span forfeits its whole cost record.
41-
# Non-numeric values bill at the base rate, as `get_aggregated_tokens`
42-
# does for the same attributes. `bool` is an `int` subclass, never a
43-
# token count.
44-
if not isinstance(v, (int, float)) or isinstance(v, bool) or not v:
37+
if not (v := get_attribute_value(attributes, self.key)):
4538
return tokens * self.base_rate
4639
if v > self.threshold:
4740
return tokens * self.new_rate

0 commit comments

Comments
 (0)