Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(langchain): structured output response parsing #2214

Merged
merged 7 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ def _set_chat_response(span: Span, response: LLMResult) -> None:
"arguments"
),
)

if generation.message.additional_kwargs.get("tool_calls"):
for idx, tool_call in enumerate(generation.message.additional_kwargs.get("tool_calls")):
tool_call_prefix = f"{prefix}.tool_calls.{idx}"

span.set_attribute(f"{tool_call_prefix}.id", tool_call.get("id"))
span.set_attribute(f"{tool_call_prefix}.name", tool_call.get("function").get("name"))
span.set_attribute(f"{tool_call_prefix}.arguments", tool_call.get("function").get("arguments"))
i += 1

if input_tokens > 0 or output_tokens > 0 or total_tokens > 0:
Expand Down Expand Up @@ -585,7 +593,7 @@ def on_llm_end(
)

_set_chat_response(span, response)
span.end()
self._end_span(span, run_id)
Copy link
Member

Choose a reason for hiding this comment

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

you're right - but it won't change anything since an LLM span is a leaf span anyway


# Record duration
duration = time.time() - self.spans[run_id].start_time
Expand All @@ -597,6 +605,7 @@ def on_llm_end(
},
)

@dont_throw
def on_tool_start(
self,
serialized: dict[str, Any],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import traceback
from opentelemetry import context as context_api
from opentelemetry.instrumentation.langchain.config import Config
from pydantic import BaseModel


class CallbackFilteredJSONEncoder(json.JSONEncoder):
Expand All @@ -13,12 +14,16 @@ def default(self, o):
if "callbacks" in o:
del o["callbacks"]
return o

if dataclasses.is_dataclass(o):
return dataclasses.asdict(o)

if hasattr(o, "to_json"):
return o.to_json()

if isinstance(o, BaseModel) and hasattr(o, "model_dump_json"):
return o.model_dump_json()

return super().default(o)


Expand Down
2,176 changes: 1,180 additions & 996 deletions packages/opentelemetry-instrumentation-langchain/poetry.lock

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions packages/opentelemetry-instrumentation-langchain/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,33 @@ opentelemetry-semantic-conventions = "^0.48b0"
opentelemetry-semantic-conventions-ai = "0.4.2"

[tool.poetry.group.dev.dependencies]
autopep8 = "^2.2.0"
flake8 = "7.0.0"
pytest = "^8.2.2"
autopep8 = "^2.3.1"
flake8 = "7.1.1"
pytest = "^8.3.3"
pytest-sugar = "1.0.0"


[tool.poetry.group.test.dependencies]
langchain = "^0.3.0"
langchain-community = "^0.3.0"
openai = "^1.31.1"
pytest = "^8.2.2"
langchain = "^0.3.4"
langchain-community = "^0.3.3"
openai = "^1.52.2"
pytest = "^8.3.3"
pytest-sugar = "1.0.0"
vcrpy = "^6.0.1"
pytest-recording = "^0.13.1"
pytest-asyncio = "^0.23.7"
vcrpy = "^6.0.2"
pytest-recording = "^0.13.2"
pytest-asyncio = "^0.24.0"
opentelemetry-sdk = "^1.27.0"
opentelemetry-instrumentation-openai = {path="../opentelemetry-instrumentation-openai", develop=true}
opentelemetry-instrumentation-bedrock = {path="../opentelemetry-instrumentation-bedrock", develop=true}
text-generation = "^0.7.0"
anthropic = "^0.34.2"
boto3 = "^1.34.120"
langchain-anthropic = "^0.2.0"
langchain-openai = "^0.2.0"
langchain-cohere = "0.3.0"
pydantic = "^2.7.1"
anthropic = "^0.37.1"
boto3 = "^1.35.49"
langchain-anthropic = "=0.2.0"
langchain-openai = "^0.2.3"
langchain-cohere = "0.3.1"
pydantic = "^2.9.2"
langchainhub = "^0.1.21"

langchainhub = "^0.1.20"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down
Loading