feat: add agent span support in langchain instrumentation#3788
feat: add agent span support in langchain instrumentation#3788yiyuan-he wants to merge 23 commits into
Conversation
| ) | ||
| span.set_attribute( | ||
| GenAI.GEN_AI_OPERATION_NAME, | ||
| "invoke_agent", |
There was a problem hiding this comment.
Is it possible that the operation name might be changed in the future, if so, declaring it a constant for it might be better.
There was a problem hiding this comment.
Good point - I'm not sure if this semantic convention will change but agree that this should be moved to a constant since it's currently hardcoded in a few places.
| GenAI.GEN_AI_OPERATION_NAME, | ||
| "invoke_agent", | ||
| ) | ||
| if agent_name: |
There was a problem hiding this comment.
If agent_name is None, in that case, should a default value be passed?
There was a problem hiding this comment.
Yeah makes sense, i'll default it to "unknown" for now. Open to suggestions on the naming though.
| span = spans[0] | ||
| assert span.name == "chain TestAgent" | ||
| assert span.kind == SpanKind.INTERNAL | ||
| assert span.attributes.get(GenAI.GEN_AI_AGENT_NAME) == "TestAgent" |
There was a problem hiding this comment.
In the above comment you mentioned that chain spans are internal operations and will not have an operation name but there you are adding assert statements for it, did I miss something?
There was a problem hiding this comment.
Thanks for your review! Let me try to clarify.
Regular chains don't have gen_ai.operation.name since they're just internal operations. However, Agent chains are different. In LangChain's architecture, agents are implemented as chains so we:
- create them using
create_agent_span()(which creates an internal span) - detect if it's actually an agent by checking for
agent_namein metadata - if it is an agent, we add the agent-specific attributes:
gen_ai.agent.namegen_ai.operation.name = "invoke_agent"
I'll update the docstrings to make this distinction clearer.
There was a problem hiding this comment.
Awesome. Thanks for the clarification.
| __all__ = ["_SpanManager", "_OPERATION_INVOKE_AGENT"] | ||
|
|
||
| # Operation name constants | ||
| _OPERATION_INVOKE_AGENT = "invoke_agent" |
There was a problem hiding this comment.
| if span: | ||
| tool = getattr(action, "tool", None) # type: ignore[arg-type] | ||
| if tool: | ||
| span.set_attribute("langchain.agent.action.tool", tool) |
There was a problem hiding this comment.
could this be expressed as one of a standard tool attributes?
(all tool attributes are in the semconv - https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/gen-ai-spans.md#execute-tool-span)
| tool_input = getattr(action, "tool_input", None) # type: ignore[arg-type] | ||
| if tool_input: | ||
| span.set_attribute( | ||
| "langchain.agent.action.tool_input", str(tool_input) |
| return_values = getattr(finish, "return_values", None) # type: ignore[arg-type] | ||
| if return_values and "output" in return_values: | ||
| span.set_attribute( | ||
| "langchain.agent.finish.output", |
There was a problem hiding this comment.
should this be either [gen_ai.tool.call.result](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/registry/attributes/gen-ai.md) or gen_ai.output.messages ?
| if agent_name: | ||
| span.set_attribute(GenAI.GEN_AI_AGENT_NAME, agent_name) | ||
|
|
||
| return span |
There was a problem hiding this comment.
Invoke agent span must have gen_ai.provider.name attribute with a name of genai system used under (e.g. openai, vertex_ai, etc). Is it possible to populate them here?
there are more recommended/opt-in attributes documented in https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/gen-ai-agent-spans.md#invoke-agent-span
lmolkova
left a comment
There was a problem hiding this comment.
Left some comments around following semconv more closely and reusing constants from the semconv lib.
|
@zhirafovod @wrisa could you please review? |
| if span: | ||
| tool = getattr(action, "tool", None) # type: ignore[arg-type] | ||
| if tool: | ||
| span.set_attribute("langchain.agent.action.tool", tool) |
There was a problem hiding this comment.
I guess your intention is to use gen_ai.tool.name, gen_ai.tool.call.result and gen_ai.tool.call.arguments which are on execute_tool. Please use these gen-ai attributes. Also another PR updating invoke_agent with the above attributes would also be required.
|
This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment. |
|
This PR has been closed due to inactivity. Please reopen if you would like to continue working on it. |
Description
This PR adds agent span support to the LangChain instrumentation following the OpenTelemetry Semantic Conventions for GenAI agent spans. It enables tracing of LangChain agent invocations, chain executions, and agent actions/decisions.
invoke_agentoperation for agent invocationsinvoke_agent {agent_name}andchain {chain_name}gen_ai.agent.name,gen_ai.operation.nameA follow-up PR will add tool execution spans (
execute_tooloperation).Sidenote: Previous PR from our team can be closed.
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
relationships
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.