Skip to content

Commit 549a50e

Browse files
committed
Fixing httpcore hook / async
1 parent 64105c3 commit 549a50e

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

examples/openai_async.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import asyncio
2+
3+
from openai import AsyncOpenAI
4+
5+
import agentwatch
6+
7+
8+
async def main():
9+
client = AsyncOpenAI()
10+
11+
completion = await client.chat.completions.create(
12+
model="gpt-4o",
13+
messages=[
14+
{
15+
"role": "user",
16+
"content": "Write a one-sentence bedtime story about a unicorn."
17+
}
18+
]
19+
)
20+
21+
print(completion.choices[0].message.content)
22+
23+
24+
25+
if __name__ == "__main__":
26+
asyncio.run(main())

src/agentwatch/hooks/http/http_base_hook.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ def _normalize_request(self, *args: Any, **kwargs: Any) -> Any:
3535
...
3636

3737
@abc.abstractmethod
38-
def _normalize_response(self, *args: Any, **kwargs: Any) -> Any:
38+
def _normalize_response_sync(self, *args: Any, **kwargs: Any) -> Any:
3939
...

src/agentwatch/hooks/http/httpcore_hook.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,26 @@ def _normalize_request(self, request: httpcore.Request) -> HookEvent:
6666
data=request_data.model_dump()
6767
)
6868

69-
def _normalize_response(self, response: httpcore.Response) -> HookEvent:
69+
async def _normalize_response(self, response: httpcore.Response) -> HookEvent:
70+
httpx_response = httpx.Response(
71+
status_code=response.status,
72+
headers=response.headers,
73+
content=await response.aread(),
74+
extensions=response.extensions,
75+
)
76+
77+
response_data = HTTPResponseData(
78+
status_code=httpx_response.status_code,
79+
headers=dict(httpx_response.headers),
80+
body=httpx_response.text
81+
)
82+
83+
return HookEvent(
84+
event_type=HookEventType.HTTP_RESPONSE,
85+
data=response_data.model_dump()
86+
)
87+
88+
def _normalize_response_sync(self, response: httpcore.Response) -> HookEvent:
7089
httpx_response = httpx.Response(
7190
status_code=response.status,
7291
headers=response.headers,
@@ -90,15 +109,15 @@ def _request_callback_sync(self, request: httpcore.Request) -> None:
90109
self._callback_handler.on_hook_callback_sync(self, normalized)
91110

92111
def _response_callback_sync(self, response: httpcore.Response) -> None:
93-
normalized = self._normalize_response(response)
112+
normalized = self._normalize_response_sync(response)
94113
self._callback_handler.on_hook_callback_sync(self, normalized)
95114

96115
async def _request_callback(self, request: httpcore.Request) -> None:
97116
normalized = self._normalize_request(request)
98117
await self._callback_handler.on_hook_callback(self, normalized)
99118

100119
async def _response_callback(self, response: httpcore.Response) -> None:
101-
normalized = self._normalize_response(response)
120+
normalized = await self._normalize_response(response)
102121
await self._callback_handler.on_hook_callback(self, normalized)
103122

104123
def _intercepted_handle_request(self, conn_self: httpcore.HTTPConnection, request: httpcore.Request) -> httpcore.Response:
@@ -118,14 +137,14 @@ def _intercepted_handle_request(self, conn_self: httpcore.HTTPConnection, reques
118137

119138
async def _intercepted_handle_async_request(self, conn_self: httpcore.AsyncHTTPConnection, request: httpcore.Request) -> httpcore.Response:
120139
await self._request_callback(request)
121-
response: httpcore.Response = self._original_handle_async_request(conn_self, request) # type: ignore
140+
response: httpcore.Response = await self._original_handle_async_request(conn_self, request) # type: ignore
122141
await self._response_callback(response)
123142

124143
# Since we messed up the response, we'll need to create a new one
125144
new_response = httpcore.Response(
126145
status=response.status,
127146
headers=response.headers,
128-
content=response.read(),
147+
content=await response.aread(),
129148
extensions=response.extensions.copy() if response.extensions else {}, # type: ignore
130149
)
131150

0 commit comments

Comments
 (0)