Skip to content
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 @@ -37,6 +37,7 @@
SpanHolder,
_set_span_attribute,
extract_model_name_from_response_metadata,
_extract_model_name_from_association_metadata,
set_chat_request,
set_chat_response,
set_chat_response_usage,
Expand Down Expand Up @@ -508,6 +509,9 @@ def on_llm_end(
_set_span_attribute(span, GEN_AI_RESPONSE_ID, id)
if model_name is None:
model_name = extract_model_name_from_response_metadata(response)
if model_name is None and hasattr(context_api, "get_value"):
association_properties = context_api.get_value("association_properties") or {}
model_name = _extract_model_name_from_association_metadata(association_properties)
token_usage = (response.llm_output or {}).get("token_usage") or (
response.llm_output or {}
).get("usage")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ def extract_model_name_from_response_metadata(response: LLMResult) -> str:
and (model_name := generation.message.response_metadata.get("model_name"))
):
return model_name


def _extract_model_name_from_association_metadata(metadata: Optional[dict[str, Any]] = None) -> str:
if metadata:
return metadata.get("ls_model_name") or "unknown"
return "unknown"


Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,4 @@ def vcr_config():
return {
"filter_headers": ["authorization", "x-api-key"],
"filter_body": ["api_key"],
"ignore_hosts": ["api.hub.langchain.com", "api.smith.langchain.com"],
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_agents_with_events_with_content(
"content": "",
"tool_calls": [
{
"id": "call_vZSljoj56JmSCeTYR9UgYkdK",
"id": "call_jb8sWc3LhC4ZZn9Ma9E2QoS4",
"function": {
"name": "tavily_search_results_json",
"arguments": {"query": "OpenLLMetry"},
Expand All @@ -137,7 +137,7 @@ def test_agents_with_events_with_content(
"message": {"content": ""},
"tool_calls": [
{
"id": "call_vZSljoj56JmSCeTYR9UgYkdK",
"id": "call_jb8sWc3LhC4ZZn9Ma9E2QoS4",
"function": {
"name": "tavily_search_results_json",
"arguments": {"query": "OpenLLMetry"},
Expand Down Expand Up @@ -198,7 +198,7 @@ def test_agents_with_events_with_no_content(
}

logs = log_exporter.get_finished_logs()
assert len(logs) == 15
assert len(logs) == 8
assert all(
log.log_record.attributes.get(GenAIAttributes.GEN_AI_SYSTEM) == "langchain"
for log in logs
Expand All @@ -217,7 +217,7 @@ def test_agents_with_events_with_no_content(
{
"tool_calls": [
{
"id": "call_vZSljoj56JmSCeTYR9UgYkdK",
"id": "call_h7poF0eyI0elJPMut3w8QcHi",
"function": {"name": "tavily_search_results_json"},
"type": "function",
}
Expand All @@ -232,7 +232,7 @@ def test_agents_with_events_with_no_content(
"message": {},
"tool_calls": [
{
"id": "call_vZSljoj56JmSCeTYR9UgYkdK",
"id": "call_h7poF0eyI0elJPMut3w8QcHi",
"function": {"name": "tavily_search_results_json"},
"type": "function",
}
Expand Down