Skip to content

Commit d60a10b

Browse files
nik13claude
andcommitted
fix(ag2): show captured content as the span's input and output
Future AGI fills a span's input and output only from input.value / output.value. AG2 sets neither: it records chat messages, tool arguments and results, and human input under their own keys. With capture_content=True, a real run on CLIProxy showed every LLM and tool span with empty input and output in Observe, though the content was on the span. The processor now copies that content to input.value / output.value (with a mime type) on chat, execute_tool and await_human_input spans. Content a TraceConfig flag hides is not copied, and existing values are kept. With capture off (the default) nothing changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d64de18 commit d60a10b

4 files changed

Lines changed: 164 additions & 4 deletions

File tree

‎python/frameworks/ag2/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
`gen_ai.operation.name` and aliases AG2's cache/thinking usage keys onto the
99
GenAI semconv names (originals kept). Does not wrap `Agent` and never
1010
imports `autogen`. Refs: TH-8236.
11+
- With `capture_content=True`, the captured chat messages, tool arguments and
12+
result, and human input are also copied to `input.value` / `output.value`,
13+
the only keys Future AGI fills a span's input and output from. Content a
14+
`TraceConfig` flag hides is not copied.
1115
- Token totals: Future AGI sums the promoted `gen_ai.usage.input_tokens` /
1216
`output_tokens` / `total_tokens` over every span of a trace. The processor
1317
moves those keys to `ag2.usage.*` (values kept) on `record_usage model_call`

‎python/frameworks/ag2/README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ the processor drops whole attributes:
148148
| `hide_input_images`, `hide_embedding_vectors` | Nothing: AG2 records only text parts (binary inputs are omitted) and no embeddings |
149149

150150
The text flags cannot redact just the text inside the JSON, so they remove the
151-
whole message attribute, which is the privacy-safe choice.
151+
whole message attribute, which is the privacy-safe choice. A removed key is
152+
not copied to `input.value` / `output.value` either.
152153

153154
## Conformance
154155

@@ -161,6 +162,7 @@ whole message attribute, which is the privacy-safe choice.
161162
| Session | Not emitted by AG2. `session.id` from traceAI's `using_session` / `using_attributes` context, which wins over a static value; or set it yourself: `setup(..., span_attributes={"session.id": "..."})` | When neither is set |
162163
| User, metadata, tags | `user.id`, `metadata`, `tag.tags` (and the other traceAI context keys) from `using_user` / `using_metadata` / `using_tags` / `using_attributes`; a key AG2 or `span_attributes` already set is not overridden | When the context does not set them |
163164
| Tools | `gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.tool.type`; arguments/result only with `capture_content=True` | |
165+
| Input / output | `input.value` / `output.value` (what Observe shows as a span's input and output), copied from the captured chat messages, tool arguments and result, or human input | Without `capture_content=True`, when a `TraceConfig` flag hides the source, and always on `invoke_agent` and `record_usage` spans (AG2 records no content there) |
164166
| Errors | OTel status `ERROR` on failing tool / model / agent spans | |
165167

166168
Notes:

‎python/frameworks/ag2/tests/test_processor.py‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,110 @@ def test_trace_config_reads_env(monkeypatch):
429429
assert "gen_ai.input.messages" not in attrs(span)
430430

431431

432+
# --- input.value / output.value --------------------------------------------
433+
# Future AGI fills a span's input and output only from input.value /
434+
# output.value, so captured AG2 content is copied there.
435+
436+
437+
@pytest.mark.parametrize(
438+
"source, expected",
439+
[
440+
(
441+
{
442+
"gen_ai.operation.name": "chat",
443+
"gen_ai.input.messages": '[{"content": "hi", "role": "user"}]',
444+
"gen_ai.output.messages": '[{"content": "yo", "role": "assistant"}]',
445+
},
446+
{
447+
"input.value": '[{"content": "hi", "role": "user"}]',
448+
"input.mime_type": "application/json",
449+
"output.value": '[{"content": "yo", "role": "assistant"}]',
450+
"output.mime_type": "application/json",
451+
},
452+
),
453+
(
454+
{
455+
"gen_ai.operation.name": "execute_tool",
456+
"gen_ai.tool.call.arguments": '{"city": "Paris"}',
457+
"gen_ai.tool.call.result": "sunny",
458+
},
459+
{
460+
"input.value": '{"city": "Paris"}',
461+
"input.mime_type": "application/json",
462+
"output.value": "sunny",
463+
"output.mime_type": "text/plain",
464+
},
465+
),
466+
(
467+
{
468+
"gen_ai.operation.name": "await_human_input",
469+
"ag2.human_input.prompt": "ok?",
470+
"ag2.human_input.response": "yes",
471+
},
472+
{
473+
"input.value": "ok?",
474+
"input.mime_type": "text/plain",
475+
"output.value": "yes",
476+
"output.mime_type": "text/plain",
477+
},
478+
),
479+
],
480+
ids=["chat", "execute_tool", "await_human_input"],
481+
)
482+
def test_captured_content_becomes_span_input_and_output(pipeline, source, expected):
483+
provider, exporter = pipeline
484+
_emit(provider, source)
485+
(span,) = exporter.get_finished_spans()
486+
got = attrs(span)
487+
assert {k: got.get(k) for k in expected} == expected
488+
for key, value in source.items():
489+
assert got[key] == value
490+
491+
492+
def test_no_input_or_output_without_captured_content(pipeline):
493+
provider, exporter = pipeline
494+
_emit(provider, {"gen_ai.operation.name": "chat", "gen_ai.request.model": "m"})
495+
(span,) = exporter.get_finished_spans()
496+
assert not {"input.value", "output.value", "input.mime_type", "output.mime_type"} & set(attrs(span))
497+
498+
499+
@pytest.mark.parametrize(
500+
"flags, gone",
501+
[
502+
({"hide_inputs": True}, {"input.value", "input.mime_type"}),
503+
({"hide_input_text": True}, {"input.value", "input.mime_type"}),
504+
({"hide_input_messages": True}, {"input.value", "input.mime_type"}),
505+
({"hide_outputs": True}, {"output.value", "output.mime_type"}),
506+
({"hide_output_text": True}, {"output.value", "output.mime_type"}),
507+
({"hide_output_messages": True}, {"output.value", "output.mime_type"}),
508+
],
509+
ids=lambda v: "+".join(sorted(v)) if isinstance(v, dict) else None,
510+
)
511+
def test_hidden_content_is_not_copied_to_input_or_output(flags, gone):
512+
chat = {k: v for k, v in _AG2_CONTENT.items() if not k.startswith(("gen_ai.tool", "ag2.human"))}
513+
got = normalize_attributes(chat, TraceConfig(**flags))
514+
assert not gone & set(got), sorted(gone & set(got))
515+
kept = {"input.value", "output.value"} - gone
516+
assert kept <= set(got), "the side that is not hidden is still copied"
517+
518+
519+
def test_existing_input_and_output_values_are_kept(pipeline):
520+
provider, exporter = pipeline
521+
_emit(
522+
provider,
523+
{
524+
"gen_ai.operation.name": "chat",
525+
"gen_ai.input.messages": "[]",
526+
"input.value": "mine",
527+
"output.value": "also mine",
528+
"gen_ai.output.messages": "[]",
529+
},
530+
)
531+
(span,) = exporter.get_finished_spans()
532+
got = attrs(span)
533+
assert (got["input.value"], got["output.value"]) == ("mine", "also mine")
534+
535+
432536
# --- installation ----------------------------------------------------------
433537

434538

‎python/frameworks/ag2/traceai_ag2/_processor.py‎

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
and neither do ``hide_llm_invocation_parameters`` (AG2 sets no
2727
``gen_ai.request.parameters``) and ``base64_image_max_length``;
2828
``TraceConfig.mask`` still runs on every key;
29+
* copies captured content to ``input.value`` / ``output.value`` (chat
30+
messages, tool arguments and result, human input), which is where Future
31+
AGI reads a span's input and output; content the config hides is not copied;
2932
* in ``on_start``, copies traceAI context attributes (``using_session``,
3033
``using_user``, ``using_metadata``, ``using_attributes``, ...) onto the span
3134
without overriding keys AG2 sets, except ``session.id``, where the context
@@ -36,6 +39,7 @@
3639

3740
from __future__ import annotations
3841

42+
import json
3943
import logging
4044
import threading
4145
from collections import OrderedDict
@@ -99,6 +103,15 @@
99103
_INPUT_CONTENT_KEYS = _INPUT_MESSAGE_KEYS + (_TOOL_ARGUMENTS, _HUMAN_INPUT_PROMPT)
100104
_OUTPUT_CONTENT_KEYS = _OUTPUT_MESSAGE_KEYS + (_TOOL_RESULT, _HUMAN_INPUT_RESPONSE)
101105

106+
# Future AGI fills a span's input and output only from ``input.value`` /
107+
# ``output.value``; AG2 sets neither. Per operation, the content keys that
108+
# become them. AG2 records no content on ``invoke_agent`` spans.
109+
_IO_SOURCES: Mapping[str, Tuple[str, str]] = {
110+
"chat": (_INPUT_MESSAGES, _OUTPUT_MESSAGES),
111+
"execute_tool": (_TOOL_ARGUMENTS, _TOOL_RESULT),
112+
"await_human_input": (_HUMAN_INPUT_PROMPT, _HUMAN_INPUT_RESPONSE),
113+
}
114+
102115

103116
def span_kind_for(attributes: Mapping[str, Any]) -> Optional[str]:
104117
"""Return the Future AGI span kind for an AG2 span, or ``None``."""
@@ -135,11 +148,45 @@ def normalize_attributes(
135148

136149
_demote_duplicate_usage(mapped, instrumented_agents)
137150

151+
hidden = _hidden_content_keys(config) if config is not None else set()
152+
_copy_content_to_io(mapped, hidden)
153+
138154
if config is not None:
139-
mapped = _apply_trace_config(mapped, config)
155+
mapped = _apply_trace_config(mapped, config, hidden)
140156
return mapped
141157

142158

159+
def _mime_type(value: Any) -> str:
160+
if isinstance(value, str):
161+
try:
162+
if isinstance(json.loads(value), (dict, list)):
163+
return "application/json"
164+
except ValueError:
165+
pass
166+
return "text/plain"
167+
168+
169+
def _copy_content_to_io(mapped: Dict[str, Any], hidden: AbstractSet[str]) -> None:
170+
"""Copy captured content to ``input.value`` / ``output.value``.
171+
172+
Only content AG2 captured and ``hidden`` does not remove is copied, so a
173+
hide flag also keeps the copy off the span. Existing values are kept.
174+
"""
175+
sources = _IO_SOURCES.get(mapped.get(SpanAttributes.GEN_AI_OPERATION_NAME)) # type: ignore[arg-type]
176+
if sources is None:
177+
return
178+
targets = (
179+
(SpanAttributes.INPUT_VALUE, SpanAttributes.INPUT_MIME_TYPE),
180+
(SpanAttributes.OUTPUT_VALUE, SpanAttributes.OUTPUT_MIME_TYPE),
181+
)
182+
for source, (value_key, mime_key) in zip(sources, targets):
183+
value = mapped.get(source)
184+
if value is None or source in hidden or value_key in mapped:
185+
continue
186+
mapped[value_key] = value
187+
mapped.setdefault(mime_key, _mime_type(value))
188+
189+
143190
# fi-collector promotes these keys into the token columns on any span
144191
# (fi-collector/pkg/adapter/adapter.go inputTokenKeys/outputTokenKeys/
145192
# totalTokenKeys), and Observe sums total_tokens over every span of a trace.
@@ -223,8 +270,11 @@ def _hidden_content_keys(config: TraceConfig) -> Set[str]:
223270
return hidden
224271

225272

226-
def _apply_trace_config(attributes: Dict[str, Any], config: TraceConfig) -> Dict[str, Any]:
227-
hidden = _hidden_content_keys(config)
273+
def _apply_trace_config(
274+
attributes: Dict[str, Any], config: TraceConfig, hidden: Optional[AbstractSet[str]] = None
275+
) -> Dict[str, Any]:
276+
if hidden is None:
277+
hidden = _hidden_content_keys(config)
228278

229279
masked: Dict[str, Any] = {}
230280
for key, value in attributes.items():

0 commit comments

Comments
 (0)