Skip to content

Commit 9b3393c

Browse files
committed
fix(openai): trace responses.retrieve so background responses get output and usage
Responses.create and Responses.parse are instrumented, Responses.retrieve is not. Background responses (background=True) return immediately with status queued and no usage, and complete through retrieve() - either directly or via responses.stream(response_id=...), which delegates to retrieve(stream=True). The result is a generation in the trace that never receives its output, token counts or cost, which reads as a free call rather than a missing one. Instrument Responses.retrieve and AsyncResponses.retrieve. The response id is recorded as the generation input, since retrieve() carries no prompt of its own; model, output and usage come from the response, so both the streaming and non-streaming paths are handled by the existing extraction code.
1 parent ab861f1 commit 9b3393c

2 files changed

Lines changed: 93 additions & 2 deletions

File tree

‎langfuse/openai.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,22 @@ class OpenAiDefinition:
175175
sync=False,
176176
min_version="1.66.0",
177177
),
178+
OpenAiDefinition(
179+
module="openai.resources.responses",
180+
object="Responses",
181+
method="retrieve",
182+
type="chat",
183+
sync=True,
184+
min_version="1.66.0",
185+
),
186+
OpenAiDefinition(
187+
module="openai.resources.responses",
188+
object="AsyncResponses",
189+
method="retrieve",
190+
type="chat",
191+
sync=False,
192+
min_version="1.66.0",
193+
),
178194
OpenAiDefinition(
179195
module="openai.resources.embeddings",
180196
object="Embeddings",
@@ -562,7 +578,11 @@ def _get_langfuse_data_from_kwargs(resource: OpenAiDefinition, kwargs: Any) -> A
562578

563579
prompt = None
564580

565-
if resource.type == "completion":
581+
if resource.method == "retrieve":
582+
# retrieve() only knows the id of a response created earlier; the model,
583+
# output and usage are filled in from the response itself.
584+
prompt = {"response_id": kwargs.get("response_id", None)}
585+
elif resource.type == "completion":
566586
prompt = kwargs.get("prompt", None)
567587
elif resource.object == "Responses" or resource.object == "AsyncResponses":
568588
prompt = _extract_responses_prompt(kwargs)

‎tests/unit/test_openai.py‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ def test_chat_completion_reasoning_parameters_absent_by_default(
12811281

12821282
def test_responses_kwargs_capture_reasoning_parameters():
12831283
data = lf_openai_module._get_langfuse_data_from_kwargs(
1284-
SimpleNamespace(type="chat", object="Responses"),
1284+
SimpleNamespace(type="chat", object="Responses", method="create"),
12851285
{
12861286
"model": "gpt-5",
12871287
"input": "1 + 1 = ?",
@@ -1531,3 +1531,74 @@ def test_with_raw_response_streaming_passes_through_untraced(
15311531
span.name != "OpenAI-generation"
15321532
for span in memory_exporter.get_finished_spans()
15331533
)
1534+
1535+
1536+
def test_response_retrieve_exports_generation_span(
1537+
langfuse_memory_client, get_span, json_attr
1538+
):
1539+
"""Background responses complete through retrieve(), which must be traced."""
1540+
openai_client = lf_openai.OpenAI(api_key="test")
1541+
response = SimpleNamespace(
1542+
model="gpt-4o-mini",
1543+
output=[{"role": "assistant", "content": "2"}],
1544+
usage=SimpleNamespace(input_tokens=3, output_tokens=1, total_tokens=4),
1545+
)
1546+
1547+
with patch.object(openai_client.responses, "_get", return_value=response):
1548+
result = openai_client.responses.retrieve(
1549+
response_id="resp_123",
1550+
name="unit-openai-response-retrieve",
1551+
)
1552+
1553+
assert result is response
1554+
1555+
langfuse_memory_client.flush()
1556+
span = get_span("unit-openai-response-retrieve")
1557+
1558+
assert span.attributes[LangfuseOtelSpanAttributes.OBSERVATION_TYPE] == "generation"
1559+
assert (
1560+
span.attributes[LangfuseOtelSpanAttributes.OBSERVATION_MODEL] == "gpt-4o-mini"
1561+
)
1562+
assert json_attr(span, LangfuseOtelSpanAttributes.OBSERVATION_INPUT) == {
1563+
"response_id": "resp_123"
1564+
}
1565+
assert json_attr(span, LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT) == {
1566+
"role": "assistant",
1567+
"content": "2",
1568+
}
1569+
assert json_attr(span, LangfuseOtelSpanAttributes.OBSERVATION_USAGE_DETAILS) == {
1570+
"input_tokens": 3,
1571+
"output_tokens": 1,
1572+
"total_tokens": 4,
1573+
}
1574+
1575+
1576+
@pytest.mark.asyncio
1577+
async def test_async_response_retrieve_exports_generation_span(
1578+
langfuse_memory_client, get_span, json_attr
1579+
):
1580+
openai_client = lf_openai.AsyncOpenAI(api_key="test")
1581+
response = SimpleNamespace(
1582+
model="gpt-4o-mini",
1583+
output=[{"role": "assistant", "content": "2"}],
1584+
usage=SimpleNamespace(input_tokens=3, output_tokens=1, total_tokens=4),
1585+
)
1586+
1587+
async def _get(*args, **kwargs):
1588+
return response
1589+
1590+
with patch.object(openai_client.responses, "_get", side_effect=_get):
1591+
result = await openai_client.responses.retrieve(
1592+
response_id="resp_123",
1593+
name="unit-openai-async-response-retrieve",
1594+
)
1595+
1596+
assert result is response
1597+
1598+
langfuse_memory_client.flush()
1599+
span = get_span("unit-openai-async-response-retrieve")
1600+
1601+
assert span.attributes[LangfuseOtelSpanAttributes.OBSERVATION_TYPE] == "generation"
1602+
assert json_attr(span, LangfuseOtelSpanAttributes.OBSERVATION_INPUT) == {
1603+
"response_id": "resp_123"
1604+
}

0 commit comments

Comments
 (0)