Skip to content
Open
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 @@ -73,14 +73,20 @@ def _set_span_completions(span, llm_request_type, choices):

if llm_request_type == LLMRequestTypeValues.CHAT:
if message is not None:
content = getattr(message, "content", message)
set_span_attribute(span, f"{prefix}.role", "assistant")
set_span_attribute(span, f"{prefix}.content", message)
set_span_attribute(span, f"{prefix}.content", content)
elif llm_request_type == LLMRequestTypeValues.COMPLETION:
set_span_attribute(span, f"{prefix}.content", message)


@dont_throw
def _set_response_attributes(span, llm_request_type, response):
# Haystack 2.x generators return their replies under a "replies" key
# (e.g. {"replies": [ChatMessage, ...]}); enumerating the dict itself
# would only iterate its keys.
if isinstance(response, dict):
response = response.get("replies")
_set_span_completions(span, llm_request_type, response)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,49 @@ def test_haystack(exporter):
)
assert SpanAttributes.TRACELOOP_ENTITY_INPUT in span_workflow.attributes
assert SpanAttributes.TRACELOOP_ENTITY_OUTPUT in span_workflow.attributes


def _run_wrapped_generator(exporter, object_name, fake_run, kwargs):
from opentelemetry.trace import get_tracer

from opentelemetry.instrumentation.haystack.wrap_openai import wrap

to_wrap = {"object": object_name, "method": "run"}
wrap(get_tracer("opentelemetry.instrumentation.haystack.test"), to_wrap)(
fake_run, None, (), kwargs
)

return exporter.get_finished_spans()[0]


def test_chat_generator_records_reply_content(exporter):
# haystack 2.x OpenAIChatGenerator.run() returns {"replies": [ChatMessage, ...]};
# the span must record the actual replies, not the "replies" key.
def fake_run(*args, **kwargs):
return {
"replies": [
ChatMessage.from_assistant("Hello there"),
ChatMessage.from_assistant("General Kenobi"),
]
}

span = _run_wrapped_generator(
exporter, "OpenAIChatGenerator", fake_run, {"messages": [ChatMessage.from_user("hi")]}
)

assert span.attributes["gen_ai.completion.0.content"] == "Hello there"
assert span.attributes["gen_ai.completion.0.role"] == "assistant"
assert span.attributes["gen_ai.completion.1.content"] == "General Kenobi"


def test_generator_records_reply_content(exporter):
# OpenAIGenerator.run() returns {"replies": [str, ...]}; same key must be unpacked.
def fake_run(*args, **kwargs):
return {"replies": ["first reply", "second reply"]}

span = _run_wrapped_generator(
exporter, "OpenAIGenerator", fake_run, {"prompt": "hi"}
)

assert span.attributes["gen_ai.completion.0.content"] == "first reply"
assert span.attributes["gen_ai.completion.1.content"] == "second reply"