Skip to content

Commit 03b01bd

Browse files
committed
Add support for openai-agents
1 parent 1708c4a commit 03b01bd

File tree

9 files changed

+610
-10
lines changed

9 files changed

+610
-10
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ requires-python = ">=3.9"
4141
[project.optional-dependencies]
4242
dev = [
4343
"openai==1.60.0",
44+
"openai-agents==0.0.3",
4445
"anthropic",
4546
"chromadb",
4647
"qdrant-client",

src/langtrace_python_sdk/instrumentation/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from .mistral import MistralInstrumentation
2525
from .ollama import OllamaInstrumentor
2626
from .openai import OpenAIInstrumentation
27+
from .openai_agents import OpenAIAgentsInstrumentation
2728
from .phidata import PhiDataInstrumentation
2829
from .pinecone import PineconeInstrumentation
2930
from .pymongo import PyMongoInstrumentation
@@ -64,4 +65,5 @@
6465
"PhiDataInstrumentation",
6566
"AgnoInstrumentation",
6667
"CleanLabInstrumentation",
68+
"OpenAIAgentsInstrumentation",
6769
]

src/langtrace_python_sdk/instrumentation/agno/instrumentation.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2024 Scale3 Labs
2+
Copyright (c) 2025 Scale3 Labs
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -14,13 +14,16 @@
1414
limitations under the License.
1515
"""
1616

17+
from typing import Collection
18+
19+
from importlib_metadata import version as v
1720
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
1821
from opentelemetry.trace import get_tracer
1922
from wrapt import wrap_function_wrapper as _W
20-
from typing import Collection
21-
from importlib_metadata import version as v
23+
2224
from .patch import patch_agent, patch_memory
2325

26+
2427
class AgnoInstrumentation(BaseInstrumentor):
2528
def instrumentation_dependencies(self) -> Collection[str]:
2629
return ["agno >= 1.1.4"]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .instrumentation import OpenAIAgentsInstrumentation
2+
3+
__all__ = [
4+
"OpenAIAgentsInstrumentation",
5+
]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Copyright (c) 2025 Scale3 Labs
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
"""
13+
14+
import importlib.metadata
15+
import logging
16+
from typing import Any, Collection, Optional
17+
18+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
19+
from opentelemetry.trace import TracerProvider, get_tracer
20+
from wrapt import wrap_function_wrapper
21+
22+
from langtrace_python_sdk.instrumentation.openai_agents.patch import \
23+
get_new_response
24+
25+
logging.basicConfig(level=logging.FATAL)
26+
27+
28+
class OpenAIAgentsInstrumentation(BaseInstrumentor): # type: ignore
29+
30+
def instrumentation_dependencies(self) -> Collection[str]:
31+
return ["openai-agents >= 0.0.3", "trace-attributes >= 4.0.5"]
32+
33+
def _instrument(self, **kwargs: Any) -> None:
34+
tracer_provider: Optional[TracerProvider] = kwargs.get("tracer_provider")
35+
tracer = get_tracer(__name__, "", tracer_provider)
36+
version: str = importlib.metadata.version("openai")
37+
38+
# TODO(Karthik): This is adding a lot of noise to the trace.
39+
# wrap_function_wrapper(
40+
# "agents.run",
41+
# "Runner._get_handoffs",
42+
# get_handoffs(version, tracer),
43+
# )
44+
45+
wrap_function_wrapper(
46+
"agents.run",
47+
"Runner._get_new_response",
48+
get_new_response(version, tracer),
49+
)
50+
51+
def _uninstrument(self, **kwargs: Any) -> None:
52+
pass

0 commit comments

Comments
 (0)