Skip to content

Commit e6f707c

Browse files
Merge branch 'main' into feat/tealtiger-governance-middleware
2 parents 0b4dfb0 + 84f8ba3 commit e6f707c

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

ag2/tools/search/perplexity.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from collections.abc import Iterable, Sequence
66
from dataclasses import dataclass, field
7-
from typing import Annotated, Any, Literal, TypeAlias
7+
from typing import Annotated, Any, Final, Literal, TypeAlias
88

99
import httpx
1010
from perplexity import AsyncPerplexity
@@ -18,6 +18,7 @@
1818
from ag2.tools.builtin._resolve import resolve_variable
1919
from ag2.tools.final import Toolkit, tool
2020
from ag2.tools.final.function_tool import FunctionTool
21+
from ag2.version import __version__
2122

2223
SonarModel: TypeAlias = Literal[
2324
"sonar",
@@ -30,6 +31,8 @@
3031
SearchContextSize: TypeAlias = Literal["low", "medium", "high"]
3132
RecencyFilter: TypeAlias = Literal["hour", "day", "week", "month", "year"]
3233

34+
_PPLX_INTEGRATION_HEADERS: Final[dict[str, str]] = {"X-Pplx-Integration": f"ag2/{__version__}"}
35+
3336

3437
@dataclass(slots=True)
3538
class PerplexitySearchResult:
@@ -101,6 +104,10 @@ def __init__(
101104
self._proxy = proxy
102105
self._verify = verify
103106
self._timeout = timeout
107+
client_kwargs["default_headers"] = {
108+
**(client_kwargs.get("default_headers") or {}),
109+
**_PPLX_INTEGRATION_HEADERS,
110+
}
104111
self._client_kwargs = client_kwargs
105112

106113
super().__init__(

test/tools/search/test_perplexity.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
pytest.importorskip("perplexity")
1414

15-
from ag2 import Agent, Context, DataInput, ImageInput, Variable
15+
from ag2 import Agent, Context, DataInput, ImageInput, Variable, __version__
1616
from ag2.events import ModelResponse, ToolCallEvent, ToolCallsEvent, ToolResultsEvent
1717
from ag2.testing import TestConfig, TrackingConfig
1818
from ag2.tools.search.perplexity import (
@@ -238,7 +238,11 @@ async def test_all_params_forwarded(self) -> None:
238238
async def test_client_kwargs_forwarded_to_sdk(self) -> None:
239239
custom_url = "https://custom.perplexity.example"
240240
route = respx.post(f"{custom_url}/search").mock(return_value=httpx.Response(200, json=_search_response()))
241-
toolkit = PerplexitySearchToolkit(api_key="test", base_url=custom_url)
241+
toolkit = PerplexitySearchToolkit(
242+
api_key="test",
243+
base_url=custom_url,
244+
default_headers={"X-Trace-Id": "abc-123"},
245+
)
242246

243247
agent = Agent(
244248
"a",
@@ -248,6 +252,8 @@ async def test_client_kwargs_forwarded_to_sdk(self) -> None:
248252
await agent.ask("search")
249253

250254
assert route.called
255+
assert route.calls.last.request.headers["X-Pplx-Integration"] == f"ag2/{__version__}"
256+
assert route.calls.last.request.headers["X-Trace-Id"] == "abc-123"
251257

252258
@respx.mock
253259
async def test_custom_tool_name_in_agent(self) -> None:
@@ -393,7 +399,11 @@ async def test_client_kwargs_forwarded_to_sdk(self) -> None:
393399
route = respx.post(f"{custom_url}/chat/completions").mock(
394400
return_value=httpx.Response(200, json=_chat_response())
395401
)
396-
toolkit = PerplexitySearchToolkit(api_key="test", base_url=custom_url)
402+
toolkit = PerplexitySearchToolkit(
403+
api_key="test",
404+
base_url=custom_url,
405+
default_headers={"X-Trace-Id": "abc-123"},
406+
)
397407

398408
agent = Agent(
399409
"a",
@@ -403,6 +413,8 @@ async def test_client_kwargs_forwarded_to_sdk(self) -> None:
403413
await agent.ask("answer")
404414

405415
assert route.called
416+
assert route.calls.last.request.headers["X-Pplx-Integration"] == f"ag2/{__version__}"
417+
assert route.calls.last.request.headers["X-Trace-Id"] == "abc-123"
406418

407419
@respx.mock
408420
async def test_custom_tool_name_in_agent(self) -> None:

0 commit comments

Comments
 (0)