Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix httpx async / decoded responses #825

Closed
wants to merge 1 commit into from

Conversation

tysonholub
Copy link
Contributor

This is a follow up from #807.

I tested v6.0.1 last week and my issue recording requests against openai was resolved. However, I just uplifted my app from Flask to FastAPI for async support and ran into an issue with v6.0.1 trying to start another event loop:

Traceback (most recent call last):
  File "/app/.venv/lib/python3.11/site-packages/openai/_base_client.py", line 880, in _request
    response = self._client.send(
               ^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/httpx/_client.py", line 915, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/httpx/_client.py", line 943, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/httpx/_client.py", line 980, in _send_handling_redirects
    response = self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/vcr/stubs/httpx_stubs.py", line 184, in _inner_send
    return _sync_vcr_send(cassette, real_send, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/vcr/stubs/httpx_stubs.py", line 177, in _sync_vcr_send
    asyncio.run(_record_responses(cassette, vcr_request, real_response, aread=False))
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 186, in run
    raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop

I also found assert not hasattr(resp, "_decoder") was preventing recording https://api.openai.com/v1/embeddings responses made by the openai client as they seem to already be decoded by the client before this code runs.

The changes in this PR resolve both issues and allow me to record openai responses in my uplifted FastAPI async app. It's not clear to me the benefit of the async refactor in httpx_stubs.py, but I'll note that my original patch in #807 continued to work in my uplifted FastAPI app.

@tysonholub
Copy link
Contributor Author

Closed in favor of #833

@tysonholub tysonholub closed this Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant