Skip to content

Commit d030990

Browse files
committed
fix precommit and typechecks
1 parent b1fe20f commit d030990

File tree

4 files changed

+42
-22
lines changed

4 files changed

+42
-22
lines changed

instrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/callback_handler.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def on_chat_model_start(
5050
messages: list[list[BaseMessage]], # type: ignore
5151
*,
5252
run_id: UUID,
53-
tags: list[str] | None,
54-
parent_run_id: UUID | None,
55-
metadata: dict[str, Any] | None,
53+
tags: list[str] | None = None,
54+
parent_run_id: UUID | None = None,
55+
metadata: dict[str, Any] | None = None,
5656
**kwargs: Any,
5757
) -> None:
5858
# Other providers/LLMs may be supported in the future and telemetry for them is skipped for now.
@@ -142,7 +142,7 @@ def on_llm_end(
142142
response: LLMResult, # type: ignore [reportUnknownParameterType]
143143
*,
144144
run_id: UUID,
145-
parent_run_id: UUID | None,
145+
parent_run_id: UUID | None = None,
146146
**kwargs: Any,
147147
) -> None:
148148
span = self.span_manager.get_span(run_id)
@@ -219,7 +219,7 @@ def on_llm_error(
219219
error: BaseException,
220220
*,
221221
run_id: UUID,
222-
parent_run_id: UUID | None,
222+
parent_run_id: UUID | None = None,
223223
**kwargs: Any,
224224
) -> None:
225225
self.span_manager.handle_error(error, run_id)
@@ -238,7 +238,11 @@ def on_chain_start(
238238
"""Run when chain starts running."""
239239
# Extract chain name from serialized or kwargs
240240
chain_name = "unknown"
241-
if serialized and "kwargs" in serialized and serialized["kwargs"].get("name"):
241+
if (
242+
serialized
243+
and "kwargs" in serialized
244+
and serialized["kwargs"].get("name")
245+
):
242246
chain_name = serialized["kwargs"]["name"]
243247
elif kwargs.get("name"):
244248
chain_name = kwargs["name"]
@@ -284,7 +288,7 @@ def on_chain_error(
284288

285289
def on_agent_action(
286290
self,
287-
action: AgentAction, # type: ignore
291+
action: AgentAction, # type: ignore[type-arg]
288292
*,
289293
run_id: UUID,
290294
parent_run_id: UUID | None = None,
@@ -296,16 +300,18 @@ def on_agent_action(
296300
# We can add attributes to the existing span if needed
297301
span = self.span_manager.get_span(run_id)
298302
if span:
299-
tool = getattr(action, "tool", None)
303+
tool = getattr(action, "tool", None) # type: ignore[arg-type]
300304
if tool:
301305
span.set_attribute("langchain.agent.action.tool", tool)
302-
tool_input = getattr(action, "tool_input", None)
306+
tool_input = getattr(action, "tool_input", None) # type: ignore[arg-type]
303307
if tool_input:
304-
span.set_attribute("langchain.agent.action.tool_input", str(tool_input))
308+
span.set_attribute(
309+
"langchain.agent.action.tool_input", str(tool_input)
310+
)
305311

306312
def on_agent_finish(
307313
self,
308-
finish: AgentFinish, # type: ignore
314+
finish: AgentFinish, # type: ignore[type-arg]
309315
*,
310316
run_id: UUID,
311317
parent_run_id: UUID | None = None,
@@ -316,6 +322,9 @@ def on_agent_finish(
316322
# Agent finish is tracked as part of the chain span
317323
span = self.span_manager.get_span(run_id)
318324
if span:
319-
return_values = getattr(finish, "return_values", None)
325+
return_values = getattr(finish, "return_values", None) # type: ignore[arg-type]
320326
if return_values and "output" in return_values:
321-
span.set_attribute("langchain.agent.finish.output", str(return_values["output"]))
327+
span.set_attribute(
328+
"langchain.agent.finish.output",
329+
str(return_values["output"]),
330+
)

instrumentation-genai/opentelemetry-instrumentation-langchain/src/opentelemetry/instrumentation/langchain/span_manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ def create_agent_span(
9898
agent_name: Optional[str] = None,
9999
) -> Span:
100100
"""Create a span for agent invocation."""
101-
span_name = f"invoke_agent {agent_name}" if agent_name else "invoke_agent"
101+
span_name = (
102+
f"invoke_agent {agent_name}" if agent_name else "invoke_agent"
103+
)
102104
span = self._create_span(
103105
run_id=run_id,
104106
parent_run_id=parent_run_id,

instrumentation-genai/opentelemetry-instrumentation-langchain/tests/test_agent_spans.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for agent-related spans in LangChain instrumentation."""
22

3-
from unittest.mock import MagicMock, Mock
3+
from unittest.mock import MagicMock
44
from uuid import uuid4
55

66
import pytest
@@ -9,7 +9,9 @@
99
from opentelemetry.instrumentation.langchain.callback_handler import (
1010
OpenTelemetryLangChainCallbackHandler,
1111
)
12-
from opentelemetry.semconv._incubating.attributes import gen_ai_attributes as GenAI
12+
from opentelemetry.semconv._incubating.attributes import (
13+
gen_ai_attributes as GenAI,
14+
)
1315
from opentelemetry.trace import SpanKind
1416

1517

@@ -26,7 +28,10 @@ def test_agent_chain_span(callback_handler, span_exporter):
2628

2729
# Start a chain that represents an agent
2830
callback_handler.on_chain_start(
29-
serialized={"name": "TestAgent", "id": ["langchain", "agents", "TestAgent"]},
31+
serialized={
32+
"name": "TestAgent",
33+
"id": ["langchain", "agents", "TestAgent"],
34+
},
3035
inputs={"input": "What is the capital of France?"},
3136
run_id=run_id,
3237
parent_run_id=parent_run_id,
@@ -99,7 +104,10 @@ def test_agent_action_tracking(callback_handler, span_exporter):
99104
span = spans[0]
100105
assert span.attributes.get("langchain.agent.action.tool") == "calculator"
101106
assert span.attributes.get("langchain.agent.action.tool_input") == "2 + 2"
102-
assert span.attributes.get("langchain.agent.finish.output") == "The answer is 4"
107+
assert (
108+
span.attributes.get("langchain.agent.finish.output")
109+
== "The answer is 4"
110+
)
103111

104112

105113
def test_regular_chain_without_agent(callback_handler, span_exporter):
@@ -131,7 +139,9 @@ def test_regular_chain_without_agent(callback_handler, span_exporter):
131139
assert span.name == "chain RegularChain"
132140
assert span.kind == SpanKind.INTERNAL
133141
assert GenAI.GEN_AI_AGENT_NAME not in span.attributes
134-
assert GenAI.GEN_AI_OPERATION_NAME not in span.attributes # Regular chains don't have operation name
142+
assert (
143+
GenAI.GEN_AI_OPERATION_NAME not in span.attributes
144+
) # Regular chains don't have operation name
135145

136146

137147
def test_chain_error_handling(callback_handler, span_exporter):
@@ -162,4 +172,4 @@ def test_chain_error_handling(callback_handler, span_exporter):
162172
span = spans[0]
163173
assert span.name == "chain ErrorChain"
164174
assert span.status.status_code.name == "ERROR"
165-
assert span.attributes.get("error.type") == "ValueError"
175+
assert span.attributes.get("error.type") == "ValueError"

instrumentation-genai/opentelemetry-instrumentation-langchain/tests/test_chain_spans.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from opentelemetry.instrumentation.langchain.callback_handler import (
88
OpenTelemetryLangChainCallbackHandler,
99
)
10-
from opentelemetry.semconv._incubating.attributes import gen_ai_attributes as GenAI
1110
from opentelemetry.trace import SpanKind
1211

1312

@@ -212,4 +211,4 @@ def test_chain_with_nested_structure(callback_handler, span_exporter):
212211
chain_names = {span.name for span in chain_spans}
213212
assert "chain MainChain" in chain_names
214213
assert "chain SubChain1" in chain_names
215-
assert "chain SubChain2" in chain_names
214+
assert "chain SubChain2" in chain_names

0 commit comments

Comments
 (0)