Skip to content

Update auto instrument langchain support#153

Merged
nikhilNava merged 7 commits intomainfrom
nikhilc/Auto-langchain-attributes
Feb 2, 2026
Merged

Update auto instrument langchain support#153
nikhilNava merged 7 commits intomainfrom
nikhilc/Auto-langchain-attributes

Conversation

@nikhilNava
Copy link
Contributor

@nikhilNava nikhilNava commented Feb 2, 2026

Task
Update auto instrumentation. Ensure input and output attributes are string or string list.

Solution
Verified against v1.2.0 langchain

{
    "name": "chat gpt-4o-mini-2024-07-18",
    "context": {
        "trace_id": "0xd59afcdf4315e39c538a8da5bf90a7e9",
        "span_id": "0x9c06c9f73df2f018",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x01422c3b27708a09",
    "start_time": "2026-02-02T17:42:20.071247Z",
    "end_time": "2026-02-02T17:42:22.174437Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "ca71ab1d-3dae-4ba8-bef6-bb10a762dbf5",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams",
        "gen_ai.operation.name": "chat",
        "gen_ai.input.messages": "[\"You are a helpful assistant that can perform calculations.\", \"Compute 15 % 4\"]",
        "gen_ai.response.id": "chatcmpl-D4sEHt78OCVTKu4ZaNSXvBfB1YpwU",
        "gen_ai.tool.arguments.0": "{\"type\": \"function\", \"function\": {\"name\": \"calculate\", \"description\": \"Perform basic arithmetic calculations. Input should be a mathematical expression.\", \"parameters\": {\"properties\": {\"expression\": {\"type\": \"string\"}}, \"required\": [\"expression\"], \"type\": \"object\"}}}",
        "gen_ai.provider.name": "azure",
        "gen_ai.request.model": "gpt-4o-mini-2024-07-18",
        "gen_ai.usage.input_tokens": 64,
        "gen_ai.usage.output_tokens": 17,
        "gen_ai.response.finish_reasons": 0
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.38.0",
            "service.namespace": "LangChainTesting",
            "service.name": "LangChainTracing"
        },
        "schema_url": ""
    }
}
{
    "name": "model_to_tools",
    "context": {
        "trace_id": "0xd59afcdf4315e39c538a8da5bf90a7e9",
        "span_id": "0x85d6ba216f935492",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x01422c3b27708a09",
    "start_time": "2026-02-02T17:42:22.178438Z",
    "end_time": "2026-02-02T17:42:22.178946Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "ca71ab1d-3dae-4ba8-bef6-bb10a762dbf5",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams",
        "gen_ai.input.messages": "[\"Compute 15 % 4\"]"
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.38.0",
            "service.namespace": "LangChainTesting",
            "service.name": "LangChainTracing"
        },
        "schema_url": ""
    }
}
{
    "name": "model",
    "context": {
        "trace_id": "0xd59afcdf4315e39c538a8da5bf90a7e9",
        "span_id": "0x01422c3b27708a09",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x70ed76d03d2e6644",
    "start_time": "2026-02-02T17:42:20.061680Z",
    "end_time": "2026-02-02T17:42:22.179960Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "ca71ab1d-3dae-4ba8-bef6-bb10a762dbf5",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams",
        "gen_ai.input.messages": "[\"Compute 15 % 4\"]"
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.38.0",
            "service.namespace": "LangChainTesting",
            "service.name": "LangChainTracing"
        },
        "schema_url": ""
    }
}
{
    "name": "execute_tool calculate",
    "context": {
        "trace_id": "0xd59afcdf4315e39c538a8da5bf90a7e9",
        "span_id": "0x06c82c89535869ae",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x7634d4a4be5f8078",
    "start_time": "2026-02-02T17:42:22.187968Z",
    "end_time": "2026-02-02T17:42:22.189960Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "ca71ab1d-3dae-4ba8-bef6-bb10a762dbf5",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams",
        "gen_ai.operation.name": "execute_tool",
        "gen_ai.tool.type": "extension",
        "gen_ai.tool.name": "calculate",
        "gen_ai.tool.description": "Perform basic arithmetic calculations. Input should be a mathematical expression.",
        "gen_ai.tool.call.id": "call_q5myB5T5bcnNqGk49bO6XzE4",
        "gen_ai.tool.arguments": "{'expression': '15 % 4'}",
        "gen_ai.event.content": "3"
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.38.0",
            "service.namespace": "LangChainTesting",
            "service.name": "LangChainTracing"
        },
        "schema_url": ""
    }
}
{
    "name": "tools_to_model",
    "context": {
        "trace_id": "0xd59afcdf4315e39c538a8da5bf90a7e9",
        "span_id": "0x2c216d250c901bd0",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x7634d4a4be5f8078",
    "start_time": "2026-02-02T17:42:22.192482Z",
    "end_time": "2026-02-02T17:42:22.192482Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "ca71ab1d-3dae-4ba8-bef6-bb10a762dbf5",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams",
        "gen_ai.input.messages": "[\"Compute 15 % 4\"]"
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.38.0",
            "service.namespace": "LangChainTesting",
            "service.name": "LangChainTracing"
        },
        "schema_url": ""
    }
}
{
    "name": "tools",
    "context": {
        "trace_id": "0xd59afcdf4315e39c538a8da5bf90a7e9",
        "span_id": "0x7634d4a4be5f8078",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x70ed76d03d2e6644",
    "start_time": "2026-02-02T17:42:22.183960Z",
    "end_time": "2026-02-02T17:42:22.192482Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "ca71ab1d-3dae-4ba8-bef6-bb10a762dbf5",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams"
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.38.0",
            "service.namespace": "LangChainTesting",
            "service.name": "LangChainTracing"
        },
        "schema_url": ""
    }
}
{
    "name": "chat AzureChatOpenAI",
    "context": {
        "trace_id": "0xd59afcdf4315e39c538a8da5bf90a7e9",
        "span_id": "0x94d90a4e9a865776",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x0ffd953cb999d4d0",
    "start_time": "2026-02-02T17:42:22.201322Z",
    "end_time": "2026-02-02T17:42:23.059171Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "ca71ab1d-3dae-4ba8-bef6-bb10a762dbf5",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams",
        "gen_ai.operation.name": "chat",
        "gen_ai.input.messages": "[\"You are a helpful assistant that can perform calculations.\", \"Compute 15 % 4\", \"3\"]",
        "gen_ai.response.id": "chatcmpl-D4sEI8t9imBEBofe0FiJNCx7RFl5c",
        "gen_ai.output.messages": "[\"The result of \\\\( 15 \\\\mod 4 \\\\) is \\\\( 3 \\\\).\"]",    
        "gen_ai.tool.arguments.0": "{\"type\": \"function\", \"function\": {\"name\": \"calculate\", \"description\": \"Perform basic arithmetic calculations. Input should be a mathematical expression.\", \"parameters\": {\"properties\": {\"expression\": {\"type\": \"string\"}}, \"required\": [\"expression\"], \"type\": \"object\"}}}",
        "gen_ai.provider.name": "azure",
        "gen_ai.request.model": "gpt-4o-mini-2024-07-18",
        "gen_ai.usage.input_tokens": 88,
        "gen_ai.usage.output_tokens": 20,
        "gen_ai.response.finish_reasons": 0
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.38.0",
            "service.namespace": "LangChainTesting",
            "service.name": "LangChainTracing"
        },
        "schema_url": ""
    }
}
{
    "name": "model_to_tools",
    "context": {
        "trace_id": "0xd59afcdf4315e39c538a8da5bf90a7e9",
        "span_id": "0x78275d2608492d48",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x0ffd953cb999d4d0",
    "start_time": "2026-02-02T17:42:23.061163Z",
    "end_time": "2026-02-02T17:42:23.062167Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "ca71ab1d-3dae-4ba8-bef6-bb10a762dbf5",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams",
        "gen_ai.input.messages": "[\"Compute 15 % 4\"]"
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.38.0",
            "service.namespace": "LangChainTesting",
            "service.name": "LangChainTracing"
        },
        "schema_url": ""
    }
}
{
    "name": "model",
    "context": {
        "trace_id": "0xd59afcdf4315e39c538a8da5bf90a7e9",
        "span_id": "0x0ffd953cb999d4d0",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x70ed76d03d2e6644",
    "start_time": "2026-02-02T17:42:22.195288Z",
    "end_time": "2026-02-02T17:42:23.062167Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "ca71ab1d-3dae-4ba8-bef6-bb10a762dbf5",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams",
        "gen_ai.input.messages": "[\"Compute 15 % 4\"]"
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.38.0",
            "service.namespace": "LangChainTesting",
            "service.name": "LangChainTracing"
        },
        "schema_url": ""
    }
}
{
    "name": "invoke_agent Perplexity02 Agent",
    "context": {
        "trace_id": "0xd59afcdf4315e39c538a8da5bf90a7e9",
        "span_id": "0x70ed76d03d2e6644",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": null,
    "start_time": "2026-02-02T17:42:20.055651Z",
    "end_time": "2026-02-02T17:42:23.063162Z",
    "status": {
        "status_code": "OK"
    },
    "attributes": {
        "operation.source": "SDK",
        "tenant.id": "badf1f56-284d-4dc5-ac59-0dd53900e743",
        "correlation.id": "7ff6dca0-917c-4bb0-b31a-794e533d8aad",
        "gen_ai.conversation.id": "ca71ab1d-3dae-4ba8-bef6-bb10a762dbf5",
        "gen_ai.agent.id": "a70248c0-9daa-4b4a-a498-b3c95c8422a5",
        "gen_ai.agent.name": "Perplexity02 Agent",
        "gen_ai.agent.upn": "Sample Agent UPN",
        "gen_ai.agent.applicationid": "Sample Blueprint ID",
        "gen_ai.caller.client.ip": "0.0.0.0",
        "gen_ai.channel.name": "msteams",
        "gen_ai.caller.id": "a92962f3-9ed4-4bcd-9ae0-ad0002b6ca76",
        "gen_ai.caller.name": "Alex Wilber",
        "gen_ai.caller.upn": "Sample UPN",
        "gen_ai.execution.type": "HumanToAgent",
        "gen_ai.input.messages": "Compute 15 % 4",
        "gen_ai.output.messages": "The result of \\( 15 \\mod 4 \\) is \\( 3 \\)."
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.38.0",
            "service.namespace": "LangChainTesting",
            "service.name": "LangChainTracing"
        },
        "schema_url": ""
    }
}

@nikhilNava nikhilNava requested a review from a team as a code owner February 2, 2026 17:50
Copilot AI review requested due to automatic review settings February 2, 2026 17:50
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the LangChain auto-instrumentation to work with langchain_core>=1.2.0 and to ensure that gen-AI input/output attributes emitted from LangChain traces conform to the expected string / string-list patterns, including new support for invoke-agent style spans coming from LangGraph.

Changes:

  • Adjusts input_messages and output_messages in utils.py to emit JSON-serialized arrays of message contents, ensuring gen_ai.input.messages / gen_ai.output.messages are strings (JSON) produced from string lists.
  • Enhances tool-related telemetry extraction in utils.py (including explicit execute-tool operation naming, tool call IDs, arguments, and results) and adds helpers for invoke-agent spans (invoke_agent_input_message, invoke_agent_output_message, set_execution_type, and improved operation-type mapping).
  • Updates tracer behavior in tracer.py to customize span naming for tools and invoke-agent/LangGraph chains and to route invoke-agent spans through the new helpers, and tightens the instrumentor dependency in tracer_instrumentor.py to langchain_core >= 1.2.0.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
libraries/microsoft-agents-a365-observability-extensions-langchain/microsoft_agents_a365/observability/extensions/langchain/utils.py Refactors message extraction to emit JSON string arrays, enriches tool call attributes (including results), adds invoke-agent message helpers and execution type tagging, and extends operation-type mapping for execute_tool and invoke_agent spans.
libraries/microsoft-agents-a365-observability-extensions-langchain/microsoft_agents_a365/observability/extensions/langchain/tracer_instrumentor.py Narrows the instrumentation dependency to langchain_core >= 1.2.0 to match and enforce the new LangChain API expectations.
libraries/microsoft-agents-a365-observability-extensions-langchain/microsoft_agents_a365/observability/extensions/langchain/tracer.py Adjusts span naming for tool and LangGraph (invoke_agent) runs and switches span enrichment to use the new invoke-agent helpers and updated message/tool extraction utilities.

@nikhilNava nikhilNava enabled auto-merge (squash) February 2, 2026 18:26
@fpfp100
Copy link

fpfp100 commented Feb 2, 2026

Where gen_ai.conversation.id is set? same question for other attributes like gen_ai.channel.name.

@fpfp100
Copy link

fpfp100 commented Feb 2, 2026

instrumentationScope is useful to check if the trace is auto instrument or manual instrument. But it is not in the output.

@nikhilNava
Copy link
Contributor Author

Where gen_ai.conversation.id is set? same question for other attributes like gen_ai.channel.name.

This is set as a baggage property on the sample app I am using.
It is set by the default span we have
libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/trace_processor/span_processor.py

@nikhilNava
Copy link
Contributor Author

instrumentationScope is useful to check if the trace is auto instrument or manual instrument. But it is not in the output.

Can you share what you mean by instrumentation scope? is it a scope langchain creates in nodejs?

@fpfp100
Copy link

fpfp100 commented Feb 2, 2026

instrumentationScope is useful to check if the trace is auto instrument or manual instrument. But it is not in the output.

Can you share what you mean by instrumentation scope? is it a scope langchain creates in nodejs?

This is the example of nodejs output :
image

@fpfp100
Copy link

fpfp100 commented Feb 2, 2026

"gen_ai.tool.arguments": "{'expression': '15 % 4'}" not a valid json output.

@nikhilNava nikhilNava merged commit 9a5dbdf into main Feb 2, 2026
14 checks passed
@nikhilNava nikhilNava deleted the nikhilc/Auto-langchain-attributes branch February 2, 2026 19:29
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.

4 participants

Comments