Skip to content

Commit 157b5c3

Browse files
fix(polygraf): align request auth/payload with API format
1 parent fad6501 commit 157b5c3

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

nemoguardrails/library/polygraf/request.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,20 @@ async def polygraf_request(
3939
Raises:
4040
ValueError: If the API call fails or the response cannot be parsed as JSON.
4141
"""
42-
payload = {"text": text}
42+
# Polygraf request payload. Some deployments accept/require additional flags
43+
# controlling PII/PID detection and aggregation.
44+
payload = {
45+
"text": text,
46+
# NOTE: Kept as `detect_pid` to match the working Polygraf API format
47+
# provided by users of this integration.
48+
"detect_pid": True,
49+
"pid_granularity": 3,
50+
"aggregate_entities": True,
51+
}
4352
headers: Dict[str, str] = {"Content-Type": "application/json"}
4453

4554
if api_key:
46-
headers["API_Key"] = f"Bearer {api_key}"
55+
headers["Authorization"] = f"Bearer {api_key}"
4756

4857
async with aiohttp.ClientSession() as session:
4958
async with session.post(
@@ -55,8 +64,20 @@ async def polygraf_request(
5564
)
5665

5766
try:
58-
return await resp.json()
67+
data = await resp.json()
5968
except aiohttp.ContentTypeError:
6069
raise ValueError(
6170
f"Failed to parse Polygraf response as JSON. Status: {resp.status}, Content: {await resp.text()}"
6271
)
72+
73+
# Polygraf may return either a raw list of entities or a wrapper object.
74+
if isinstance(data, list):
75+
return data
76+
if isinstance(data, dict):
77+
entities = data.get("entities")
78+
if isinstance(entities, list):
79+
return entities
80+
81+
raise ValueError(
82+
"Invalid response from Polygraf service: expected a list of entities or an object with an 'entities' list."
83+
)

0 commit comments

Comments
 (0)