Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5936e09
test(harness): add shared OTLP harness for TH-8103 contract tests
nik13 Oct 4, 2026
32f5c9d
test(harness): accept the collector /tracer/v1/traces path
nik13 Oct 4, 2026
3eaadc8
fix(harness): decode chunked OTLP bodies and record request metadata
nik13 Oct 4, 2026
8bfc3de
feat(ag2-classic): add traceAI-ag2-classic native enabler for autogen…
nik13 Oct 4, 2026
96ef5b3
fix(ag2-classic): keep promoted token keys on LLM spans only
nik13 Oct 4, 2026
10e9dd7
fix(ag2-classic): keep content dropping after uninstrument
nik13 Oct 4, 2026
e4540e4
fix(ag2-classic): roll back the LLM patch when setup() fails
nik13 Oct 4, 2026
911cabf
fix(ag2-classic): keep the processor first across add_span_processor
nik13 Oct 4, 2026
5805ca0
fix(ag2-classic): follow non-AG2 spans when finding the outer convers…
nik13 Oct 4, 2026
a383f20
fix(ag2-classic): apply using_attributes context to AG2 spans
nik13 Oct 5, 2026
cb44a2f
fix(ag2-classic): keep every gen_ai.usage.* key off non-LLM spans
nik13 Oct 5, 2026
54a587c
test(ag2-classic): cover the LLM exception path
nik13 Oct 5, 2026
af1845a
docs(ag2-classic): describe the autogen 0.14.0 alias wheel correctly
nik13 Oct 5, 2026
2aee53b
fix(ag2-classic): reopen a disabled processor when setup() reuses it
nik13 Oct 5, 2026
9df5068
fix(ag2-classic): warn when the provider runs span processors concurr…
nik13 Oct 5, 2026
6410498
fix(ag2-classic): keep a later OpenAIWrapper.create wrapper on uninst…
nik13 Oct 5, 2026
0af3c23
docs(ag2-classic): install autogen with the openai extra
nik13 Oct 5, 2026
9c2d57d
docs(ag2-classic): say which spans keep the chat id under using_session
nik13 Oct 5, 2026
2f1a5fc
docs(ag2-classic): document the live-span tracking cap
nik13 Oct 5, 2026
d84bf89
fix(ag2-classic): leave a zero AG2 cost to Future AGI's pricing
nik13 Oct 12, 2026
a68ed5b
fix(ag2-classic): require fi-instrumentation-otel>=1.1.0
nik13 Oct 12, 2026
695f8b8
fix(ag2-classic): follow the traceAI TraceConfig content convention
nik13 Oct 17, 2026
fd41536
Merge dev into feat/th-8237-ag2-classic
nik13 Oct 21, 2026
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
18 changes: 18 additions & 0 deletions python/frameworks/ag2-classic/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## [0.1.0] - 2026-10-04
### Features
- New package for AG2 Classic (`autogen` 0.14.x). `setup()` enables upstream
`autogen.opentelemetry` (`instrument_llm_wrapper`, `instrument_agent`,
`instrument_pattern`) on the Future AGI tracer provider, without creating a
second provider or exporter.
- `AG2ClassicSpanProcessor` maps `ag2.span.type` to `gen_ai.span.kind`, copies
the outermost `gen_ai.conversation.id` to `session.id`, adds
`gen_ai.cost.total` (only when upstream's cost is above 0; AG2 reports 0
for models it cannot price) and `gen_ai.usage.total_tokens` on LLM spans,
and sets ERROR status from upstream `error.type`.
- Content follows the traceAI `TraceConfig` convention: captured by default,
hidden with `FI_HIDE_INPUTS` / `FI_HIDE_OUTPUTS` (and the message/text
variants) or `setup(config=TraceConfig(...))`. Upstream message, tool,
human-input and code-output keys of a hidden side are removed before export.
`capture_content=True/False` is an optional override.
- Version guard: rejects non-0.14 `autogen`, Microsoft AutoGen, `ag2` 1.x and
`pyautogen` layouts with an error that names the matching package.
232 changes: 232 additions & 0 deletions python/frameworks/ag2-classic/README.md

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions python/frameworks/ag2-classic/examples/basic_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Two-agent AG2 Classic chat with a tool, traced to Future AGI.

Install: pip install traceAI-ag2-classic "autogen[openai]"
(plain autogen has no OpenAI client; the openai extra adds it).

Requires FI_API_KEY, FI_SECRET_KEY and OPENAI_API_KEY in the environment.
This example calls a real model; the package tests use a loopback fake instead.
"""

import os

from autogen import ConversableAgent
from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType

from traceai_ag2_classic import setup


def get_weather(city: str) -> str:
"""Return the weather for a city."""
return f"sunny in {city}"


def main() -> None:
trace_provider = register(
project_type=ProjectType.OBSERVE,
project_name=os.getenv("FI_PROJECT_NAME", "ag2-classic-example"),
)

llm_config = {"config_list": [{"model": "gpt-4o-mini", "api_key": os.environ["OPENAI_API_KEY"]}]}
assistant = ConversableAgent(
"assistant",
llm_config=llm_config,
human_input_mode="NEVER",
is_termination_msg=lambda m: "TERMINATE" in (m.get("content") or ""),
system_message="Use the weather tool, answer, then say TERMINATE.",
)
user = ConversableAgent("user", llm_config=False, human_input_mode="NEVER", max_consecutive_auto_reply=3)
assistant.register_for_llm(description="Get the weather")(get_weather)
user.register_for_execution()(get_weather)

# Content (messages, tool arguments/results) is exported unless FI_HIDE_INPUTS /
# FI_HIDE_OUTPUTS are set (traceAI TraceConfig convention).
setup(tracer_provider=trace_provider, agents=[assistant, user])

user.initiate_chat(assistant, message="What is the weather in Paris?", max_turns=3)
trace_provider.force_flush()


if __name__ == "__main__":
main()
47 changes: 47 additions & 0 deletions python/frameworks/ag2-classic/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[tool.poetry]
name = "traceAI-ag2-classic"
version = "0.1.0"
description = "Future AGI tracing for AG2 Classic (autogen 0.14.x) through its built-in autogen.opentelemetry"
authors = ["Future AGI <no-reply@futureagi.com>"]
license = "Apache-2.0"
readme = "README.md"
homepage = "https://github.com/future-agi/traceAI"
repository = "https://github.com/future-agi/traceAI"
keywords = ["opentelemetry", "ag2", "autogen", "ag2-classic", "tracing", "observability", "agents"]
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: System :: Monitoring",
]
packages = [
{ include = "traceai_ag2_classic" },
]

[tool.poetry.dependencies]
python = ">=3.10,<3.14"
# AG2 Classic: the `autogen` distribution only. Never vendored or bundled.
# No dependency on `ag2` 1.x, `pyautogen` or `autogen-agentchat`.
autogen = ">=0.14.0,<0.15"
fi-instrumentation-otel = ">=1.1.0"
opentelemetry-api = ">=1.29.0"
opentelemetry-sdk = ">=1.29.0"

[tool.poetry.group.dev.dependencies]
pytest = ">=8.0.0"
openai = ">=2.44.0,<3.0"

[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py"]
python_functions = ["test_*"]

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
62 changes: 62 additions & 0 deletions python/frameworks/ag2-classic/tests/_contract_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Contract app: real AG2 Classic + real OTel SDK + real Future AGI OTLP/HTTP exporter.

Run as a subprocess by ``test_contract_harness.py``. The parent points
``FI_BASE_URL`` at a loopback harness ``Receiver``; ``register()`` then exports
OTLP/HTTP protobuf to ``{FI_BASE_URL}/tracer/v1/traces``. The model is a
loopback fake. Usage: ``python _contract_app.py off|on``.
"""

from __future__ import annotations

import json
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
if HERE not in sys.path:
sys.path.insert(0, HERE)


def main(argv: list) -> int:
capture = len(argv) > 1 and argv[1] == "on"

from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType
from opentelemetry import trace as trace_api

from _fake_openai import FakeOpenAI
from _scenarios import TOOL_SECRET_CITY, broken_tool_chat, failing_llm_chat, group_chat, two_agent_tool_chat
from traceai_ag2_classic import setup

global_before = type(trace_api.get_tracer_provider()).__name__
provider = register(
project_type=ProjectType.OBSERVE,
project_name="ag2-classic-contract",
verbose=False,
)
tracing = setup(tracer_provider=provider, capture_content=capture)

out = {}
with FakeOpenAI(tool_arguments={"city": TOOL_SECRET_CITY}) as fake:
chat = two_agent_tool_chat(tracing, fake)
out["chat_id"] = str(chat.chat_id)
broken_tool_chat(tracing, fake)
group = group_chat(tracing, fake)
out["group_chat_id"] = str(group.chat_id)
# The model rejects this one (HTTP 400): an LLM span that ends in an exception.
out["llm_error"] = failing_llm_chat(tracing, fake)
out["llm_requests"] = len(fake.requests) # successful model calls only
out["llm_failed_requests"] = len(fake.failed_requests)

flushed = provider.force_flush(timeout_millis=10000)
out["flushed"] = bool(flushed)
out["global_provider_before"] = global_before
out["global_provider_after"] = type(trace_api.get_tracer_provider()).__name__
out["processors"] = [type(p).__name__ for p in provider._active_span_processor._span_processors]
provider.shutdown()
print("CONTRACT_RESULT " + json.dumps(out))
return 0


if __name__ == "__main__":
sys.exit(main(sys.argv))
162 changes: 162 additions & 0 deletions python/frameworks/ag2-classic/tests/_fake_openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
"""A loopback-only, OpenAI-compatible fake chat completions server for tests.

No network and no vendor API: the server binds to 127.0.0.1 on a free port and
answers ``POST /v1/chat/completions`` with deterministic replies chosen from the
request body:

* a request whose messages contain ``LLM_FAIL_TRIGGER`` gets HTTP 400 (the
OpenAI client raises ``BadRequestError`` and does not retry); it is recorded
in ``failed_requests``, not ``requests``;
* a group-chat speaker-selection prompt gets ``speaker_reply`` (an agent name);
* a request whose last message is a tool result gets ``final_reply``;
* a request that offers tools gets one tool call to the first offered tool;
* anything else gets ``text_reply``.

Every successful reply carries ``usage`` so token attributes can be asserted.
"""

from __future__ import annotations

import json
import threading
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from typing import Any, Dict, List, Optional

PROMPT_TOKENS = 11
COMPLETION_TOKENS = 7
FAKE_MODEL = "gpt-4o-mini"
LLM_FAIL_TRIGGER = "FAKE-LLM-REJECT"


class FakeOpenAI:
def __init__(
self,
*,
text_reply: str = "Hello from the fake model. TERMINATE",
final_reply: str = "Tool says sunny. TERMINATE",
speaker_reply: str = "critic",
tool_arguments: Optional[Dict[str, Any]] = None,
) -> None:
self.text_reply = text_reply
self.final_reply = final_reply
self.speaker_reply = speaker_reply
self.tool_arguments = tool_arguments if tool_arguments is not None else {"city": "Paris"}
self.requests: List[Dict[str, Any]] = []
self.failed_requests: List[Dict[str, Any]] = []
self._lock = threading.Lock()
self._counter = 0
owner = self

class Handler(BaseHTTPRequestHandler):
def do_POST(self) -> None: # noqa: N802
if not self.path.rstrip("/").endswith("/chat/completions"):
self.send_error(HTTPStatus.NOT_FOUND)
return
length = int(self.headers.get("Content-Length", "0"))
body = json.loads(self.rfile.read(length).decode("utf-8") or "{}")
status = HTTPStatus.OK
if owner._rejects(body):
status = HTTPStatus.BAD_REQUEST
payload = {
"error": {
"message": "fake model rejected the request",
"type": "invalid_request_error",
"param": None,
"code": "fake_rejected",
}
}
else:
payload = owner._reply(body)
data = json.dumps(payload).encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)

def log_message(self, format: str, *args: Any) -> None: # noqa: A002
return

self._server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
self.base_url = "http://127.0.0.1:{0}/v1".format(self._server.server_port)
self._thread = threading.Thread(target=self._server.serve_forever, daemon=True)
self._thread.start()

# Context manager -------------------------------------------------------

def __enter__(self) -> "FakeOpenAI":
return self

def __exit__(self, *_exc: Any) -> None:
self.close()

def close(self) -> None:
self._server.shutdown()
self._server.server_close()
self._thread.join()

# Helpers ---------------------------------------------------------------

def llm_config(self, **extra: Any) -> Dict[str, Any]:
entry = {
"api_type": "openai",
"model": FAKE_MODEL,
"api_key": "sk-placeholder-not-a-real-key",
"base_url": self.base_url,
}
entry.update(extra)
return {"config_list": [entry], "cache_seed": None}

def _rejects(self, body: Dict[str, Any]) -> bool:
messages = body.get("messages") or []
if not any(LLM_FAIL_TRIGGER in str(m.get("content") or "") for m in messages if isinstance(m, dict)):
return False
with self._lock:
self.failed_requests.append(body)
return True

def _reply(self, body: Dict[str, Any]) -> Dict[str, Any]:
with self._lock:
self.requests.append(body)
self._counter += 1
n = self._counter
messages = body.get("messages") or []
joined = " ".join(str(m.get("content") or "") for m in messages if isinstance(m, dict))
last = messages[-1] if messages else {}

message: Dict[str, Any]
finish = "stop"
if "select the next role" in joined or "Then select the next role" in joined:
message = {"role": "assistant", "content": self.speaker_reply}
elif isinstance(last, dict) and last.get("role") == "tool":
message = {"role": "assistant", "content": self.final_reply}
elif body.get("tools"):
tool = body["tools"][0]["function"]["name"]
message = {
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_fake_{0}".format(n),
"type": "function",
"function": {"name": tool, "arguments": json.dumps(self.tool_arguments)},
}
],
}
finish = "tool_calls"
else:
message = {"role": "assistant", "content": self.text_reply}

return {
"id": "chatcmpl-fake-{0}".format(n),
"object": "chat.completion",
"created": 1700000000,
"model": body.get("model") or FAKE_MODEL,
"choices": [{"index": 0, "message": message, "finish_reason": finish}],
"usage": {
"prompt_tokens": PROMPT_TOKENS,
"completion_tokens": COMPLETION_TOKENS,
"total_tokens": PROMPT_TOKENS + COMPLETION_TOKENS,
},
}
Loading
Loading