Skip to content

fix(azure-ai): fall back to message metadata for gen_ai.response.id - #802

Merged
Facundo Santiago (santiagxf) merged 1 commit into
langchain-ai:mainfrom
0xDevNinja:fix/azure-ai-response-id-fallback
Jul 5, 2026
Merged

fix(azure-ai): fall back to message metadata for gen_ai.response.id#802
Facundo Santiago (santiagxf) merged 1 commit into
langchain-ai:mainfrom
0xDevNinja:fix/azure-ai-response-id-fallback

Conversation

@0xDevNinja

Copy link
Copy Markdown
Contributor

Fixes #655.

Problem

AzureAIOpenTelemetryTracer.on_llm_end only stamps gen_ai.response.id when it finds LLMResult.llm_output["id"]. That field is populated on the Chat Completions path, but on the Responses API path langchain-openai exposes the id only on AIMessage.response_metadata["id"]. As a result gen_ai.response.id is missing on that path, and the traces do not show up in the Foundry portal.

Fix

Resolve the response id with a fallback: use llm_output["id"] when present, otherwise walk the chat generations and read AIMessage.response_metadata["id"]. The attribute is now emitted on both paths, and llm_output["id"] still takes precedence.

response_id = llm_output.get("id")
if not response_id:
    for gen in chat_generations:
        message = getattr(gen, "message", None)
        metadata = getattr(message, "response_metadata", None)
        if isinstance(metadata, Mapping):
            response_id = metadata.get("id")
            if response_id:
                break
if response_id:
    record.span.set_attribute(Attrs.RESPONSE_ID, str(response_id))

Tests

Added two unit tests in tests/unit_tests/test_inference_tracing.py:

  • test_response_id_falls_back_to_message_response_metadata — id only on AIMessage.response_metadata is now emitted.
  • test_response_id_prefers_llm_output_over_message_metadatallm_output["id"] wins when both are present.

Full test_inference_tracing.py module passes (138 tests); ruff + mypy clean.

Fixes #655

The AzureAIOpenTelemetryTracer only stamped `gen_ai.response.id` from
`LLMResult.llm_output["id"]`, which is populated on the Chat Completions
path. On the Responses API path the id is only available on
`AIMessage.response_metadata["id"]`, so the attribute was missing and
traces did not show up in the Foundry portal.

Resolve the id with a fallback to the message metadata so it is emitted on
both paths. `llm_output["id"]` still takes precedence when present.

Fixes langchain-ai#655
@santiagxf
Facundo Santiago (santiagxf) merged commit 1afe242 into langchain-ai:main Jul 5, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trace missing gen_ai.response.id property for chat completions api

2 participants