Guest User

Streaming response wrapping for custom transport layers (error 1)

a guest
Feb 7th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.52 KB | Source Code | 0 0
  1. ---------------------------------------------------------------------------
  2. AttributeError                            Traceback (most recent call last)
  3. File /lib/python3.11/site-packages/openai/_base_client.py:882, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
  4.     881 try:
  5. --> 882     response = self._client.send(
  6.     883         request,
  7.     884         auth=self.custom_auth,
  8.     885         stream=stream or self._should_stream_response_body(request=request),
  9.     886     )
  10.     887     log.debug(
  11.     888         'HTTP Request: %s %s "%i %s"', request.method, request.url, response.status_code, response.reason_phrase
  12.     889     )
  13.  
  14. File /lib/python3.11/site-packages/httpx/_client.py:915, in Client.send(self, request, stream, auth, follow_redirects)
  15.     913 auth = self._build_request_auth(request, auth)
  16. --> 915 response = self._send_handling_auth(
  17.     916     request,
  18.     917     auth=auth,
  19.     918     follow_redirects=follow_redirects,
  20.     919     history=[],
  21.     920 )
  22.     921 try:
  23.  
  24. File /lib/python3.11/site-packages/httpx/_client.py:943, in Client._send_handling_auth(self, request, auth, follow_redirects, history)
  25.     942 while True:
  26. --> 943     response = self._send_handling_redirects(
  27.     944         request,
  28.     945         follow_redirects=follow_redirects,
  29.     946         history=history,
  30.     947     )
  31.     948     try:
  32.  
  33. File /lib/python3.11/site-packages/httpx/_client.py:980, in Client._send_handling_redirects(self, request, follow_redirects, history)
  34.     978     hook(request)
  35. --> 980 response = self._send_single_request(request)
  36.     981 try:
  37.  
  38. File /lib/python3.11/site-packages/httpx/_client.py:1016, in Client._send_single_request(self, request)
  39.    1015 with request_context(request=request):
  40. -> 1016     response = transport.handle_request(request)
  41.    1018 assert isinstance(response.stream, SyncByteStream)
  42.  
  43. Cell In[2], line 44, in URLLib3Transport.handle_request(self, request)
  44.      43 payload = json.loads(request.content.decode("utf-8").replace("'",'"'))
  45. ---> 44 urllib3_response = self._pool.request(
  46.      45     method=request.method,
  47.      46     url=str(request.url),
  48.      47     headers=request.headers,
  49.      48     stream=True,
  50.      49 )
  51.      50 headers = [(b"content-type", b"application/json")]
  52.  
  53. AttributeError: 'ConnectionPool' object has no attribute 'request'
  54.  
  55. During handling of the above exception, another exception occurred:
  56.  
  57. AttributeError                            Traceback (most recent call last)
  58. File /lib/python3.11/site-packages/openai/_base_client.py:882, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
  59.     881 try:
  60. --> 882     response = self._client.send(
  61.     883         request,
  62.     884         auth=self.custom_auth,
  63.     885         stream=stream or self._should_stream_response_body(request=request),
  64.     886     )
  65.     887     log.debug(
  66.     888         'HTTP Request: %s %s "%i %s"', request.method, request.url, response.status_code, response.reason_phrase
  67.     889     )
  68.  
  69. File /lib/python3.11/site-packages/httpx/_client.py:915, in Client.send(self, request, stream, auth, follow_redirects)
  70.     913 auth = self._build_request_auth(request, auth)
  71. --> 915 response = self._send_handling_auth(
  72.     916     request,
  73.     917     auth=auth,
  74.     918     follow_redirects=follow_redirects,
  75.     919     history=[],
  76.     920 )
  77.     921 try:
  78.  
  79. File /lib/python3.11/site-packages/httpx/_client.py:943, in Client._send_handling_auth(self, request, auth, follow_redirects, history)
  80.     942 while True:
  81. --> 943     response = self._send_handling_redirects(
  82.     944         request,
  83.     945         follow_redirects=follow_redirects,
  84.     946         history=history,
  85.     947     )
  86.     948     try:
  87.  
  88. File /lib/python3.11/site-packages/httpx/_client.py:980, in Client._send_handling_redirects(self, request, follow_redirects, history)
  89.     978     hook(request)
  90. --> 980 response = self._send_single_request(request)
  91.     981 try:
  92.  
  93. File /lib/python3.11/site-packages/httpx/_client.py:1016, in Client._send_single_request(self, request)
  94.    1015 with request_context(request=request):
  95. -> 1016     response = transport.handle_request(request)
  96.    1018 assert isinstance(response.stream, SyncByteStream)
  97.  
  98. Cell In[2], line 44, in URLLib3Transport.handle_request(self, request)
  99.      43 payload = json.loads(request.content.decode("utf-8").replace("'",'"'))
  100. ---> 44 urllib3_response = self._pool.request(
  101.      45     method=request.method,
  102.      46     url=str(request.url),
  103.      47     headers=request.headers,
  104.      48     stream=True,
  105.      49 )
  106.      50 headers = [(b"content-type", b"application/json")]
  107.  
  108. AttributeError: 'ConnectionPool' object has no attribute 'request'
  109.  
  110. During handling of the above exception, another exception occurred:
  111.  
  112. AttributeError                            Traceback (most recent call last)
  113. File /lib/python3.11/site-packages/openai/_base_client.py:882, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
  114.     881 try:
  115. --> 882     response = self._client.send(
  116.     883         request,
  117.     884         auth=self.custom_auth,
  118.     885         stream=stream or self._should_stream_response_body(request=request),
  119.     886     )
  120.     887     log.debug(
  121.     888         'HTTP Request: %s %s "%i %s"', request.method, request.url, response.status_code, response.reason_phrase
  122.     889     )
  123.  
  124. File /lib/python3.11/site-packages/httpx/_client.py:915, in Client.send(self, request, stream, auth, follow_redirects)
  125.     913 auth = self._build_request_auth(request, auth)
  126. --> 915 response = self._send_handling_auth(
  127.     916     request,
  128.     917     auth=auth,
  129.     918     follow_redirects=follow_redirects,
  130.     919     history=[],
  131.     920 )
  132.     921 try:
  133.  
  134. File /lib/python3.11/site-packages/httpx/_client.py:943, in Client._send_handling_auth(self, request, auth, follow_redirects, history)
  135.     942 while True:
  136. --> 943     response = self._send_handling_redirects(
  137.     944         request,
  138.     945         follow_redirects=follow_redirects,
  139.     946         history=history,
  140.     947     )
  141.     948     try:
  142.  
  143. File /lib/python3.11/site-packages/httpx/_client.py:980, in Client._send_handling_redirects(self, request, follow_redirects, history)
  144.     978     hook(request)
  145. --> 980 response = self._send_single_request(request)
  146.     981 try:
  147.  
  148. File /lib/python3.11/site-packages/httpx/_client.py:1016, in Client._send_single_request(self, request)
  149.    1015 with request_context(request=request):
  150. -> 1016     response = transport.handle_request(request)
  151.    1018 assert isinstance(response.stream, SyncByteStream)
  152.  
  153. Cell In[2], line 44, in URLLib3Transport.handle_request(self, request)
  154.      43 payload = json.loads(request.content.decode("utf-8").replace("'",'"'))
  155. ---> 44 urllib3_response = self._pool.request(
  156.      45     method=request.method,
  157.      46     url=str(request.url),
  158.      47     headers=request.headers,
  159.      48     stream=True,
  160.      49 )
  161.      50 headers = [(b"content-type", b"application/json")]
  162.  
  163. AttributeError: 'ConnectionPool' object has no attribute 'request'
  164.  
  165. The above exception was the direct cause of the following exception:
  166.  
  167. APIConnectionError                        Traceback (most recent call last)
  168. Cell In[4], line 1
  169. ----> 1 response = openai_client.chat.completions.with_raw_response.create(
  170.       2     messages=[{
  171.       3         "role": "user",
  172.       4         "content": "sing me a song",
  173.       5     }],
  174.       6     model="gpt-35-turbo",
  175.       7     max_tokens=30,
  176.       8     temperature=0.7,
  177.       9     stream=True
  178.      10 )
  179.  
  180. File /lib/python3.11/site-packages/openai/_response.py:244, in to_raw_response_wrapper.<locals>.wrapped(*args, **kwargs)
  181.     240 extra_headers[RAW_RESPONSE_HEADER] = "true"
  182.     242 kwargs["extra_headers"] = extra_headers
  183. --> 244 return cast(APIResponse[R], func(*args, **kwargs))
  184.  
  185. File /lib/python3.11/site-packages/openai/_utils/_utils.py:301, in required_args.<locals>.inner.<locals>.wrapper(*args, **kwargs)
  186.     299             msg = f"Missing required argument: {quote(missing[0])}"
  187.     300     raise TypeError(msg)
  188. --> 301 return func(*args, **kwargs)
  189.  
  190. File /lib/python3.11/site-packages/openai/resources/chat/completions.py:598, in Completions.create(self, messages, model, frequency_penalty, function_call, functions, logit_bias, max_tokens, n, presence_penalty, response_format, seed, stop, stream, temperature, tool_choice, tools, top_p, user, extra_headers, extra_query, extra_body, timeout)
  191.     551 @required_args(["messages", "model"], ["messages", "model", "stream"])
  192.     552 def create(
  193.     553     self,
  194.    (...)
  195.     596     timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
  196.     597 ) -> ChatCompletion | Stream[ChatCompletionChunk]:
  197. --> 598     return self._post(
  198.     599         "/chat/completions",
  199.     600         body=maybe_transform(
  200.     601             {
  201.     602                 "messages": messages,
  202.     603                 "model": model,
  203.     604                 "frequency_penalty": frequency_penalty,
  204.     605                 "function_call": function_call,
  205.     606                 "functions": functions,
  206.     607                 "logit_bias": logit_bias,
  207.     608                 "max_tokens": max_tokens,
  208.     609                 "n": n,
  209.     610                 "presence_penalty": presence_penalty,
  210.     611                 "response_format": response_format,
  211.     612                 "seed": seed,
  212.     613                 "stop": stop,
  213.     614                 "stream": stream,
  214.     615                 "temperature": temperature,
  215.     616                 "tool_choice": tool_choice,
  216.     617                 "tools": tools,
  217.     618                 "top_p": top_p,
  218.     619                 "user": user,
  219.     620             },
  220.     621             completion_create_params.CompletionCreateParams,
  221.     622         ),
  222.     623         options=make_request_options(
  223.     624             extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  224.     625         ),
  225.     626         cast_to=ChatCompletion,
  226.     627         stream=stream or False,
  227.     628         stream_cls=Stream[ChatCompletionChunk],
  228.     629     )
  229.  
  230. File /lib/python3.11/site-packages/openai/_base_client.py:1096, in SyncAPIClient.post(self, path, cast_to, body, options, files, stream, stream_cls)
  231.    1082 def post(
  232.    1083     self,
  233.    1084     path: str,
  234.    (...)
  235.    1091     stream_cls: type[_StreamT] | None = None,
  236.    1092 ) -> ResponseT | _StreamT:
  237.    1093     opts = FinalRequestOptions.construct(
  238.    1094         method="post", url=path, json_data=body, files=to_httpx_files(files), **options
  239.    1095     )
  240. -> 1096     return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  241.  
  242. File /lib/python3.11/site-packages/openai/_base_client.py:856, in SyncAPIClient.request(self, cast_to, options, remaining_retries, stream, stream_cls)
  243.     847 def request(
  244.     848     self,
  245.     849     cast_to: Type[ResponseT],
  246.    (...)
  247.     854     stream_cls: type[_StreamT] | None = None,
  248.     855 ) -> ResponseT | _StreamT:
  249. --> 856     return self._request(
  250.     857         cast_to=cast_to,
  251.     858         options=options,
  252.     859         stream=stream,
  253.     860         stream_cls=stream_cls,
  254.     861         remaining_retries=remaining_retries,
  255.     862     )
  256.  
  257. File /lib/python3.11/site-packages/openai/_base_client.py:929, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
  258.     926         response.close()
  259.     928     if retries > 0:
  260. --> 929         return self._retry_request(
  261.     930             options,
  262.     931             cast_to,
  263.     932             retries,
  264.     933             stream=stream,
  265.     934             stream_cls=stream_cls,
  266.     935             response_headers=response.headers if response is not None else None,
  267.     936         )
  268.     938     raise APIConnectionError(request=request) from err
  269.     940 return self._process_response(
  270.     941     cast_to=cast_to,
  271.     942     options=options,
  272.    (...)
  273.     945     stream_cls=stream_cls,
  274.     946 )
  275.  
  276. File /lib/python3.11/site-packages/openai/_base_client.py:966, in SyncAPIClient._retry_request(self, options, cast_to, remaining_retries, response_headers, stream, stream_cls)
  277.     962 # In a synchronous context we are blocking the entire thread. Up to the library user to run the client in a
  278.     963 # different thread if necessary.
  279.     964 time.sleep(timeout)
  280. --> 966 return self._request(
  281.     967     options=options,
  282.     968     cast_to=cast_to,
  283.     969     remaining_retries=remaining,
  284.     970     stream=stream,
  285.     971     stream_cls=stream_cls,
  286.     972 )
  287.  
  288. File /lib/python3.11/site-packages/openai/_base_client.py:929, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
  289.     926         response.close()
  290.     928     if retries > 0:
  291. --> 929         return self._retry_request(
  292.     930             options,
  293.     931             cast_to,
  294.     932             retries,
  295.     933             stream=stream,
  296.     934             stream_cls=stream_cls,
  297.     935             response_headers=response.headers if response is not None else None,
  298.     936         )
  299.     938     raise APIConnectionError(request=request) from err
  300.     940 return self._process_response(
  301.     941     cast_to=cast_to,
  302.     942     options=options,
  303.    (...)
  304.     945     stream_cls=stream_cls,
  305.     946 )
  306.  
  307. File /lib/python3.11/site-packages/openai/_base_client.py:966, in SyncAPIClient._retry_request(self, options, cast_to, remaining_retries, response_headers, stream, stream_cls)
  308.     962 # In a synchronous context we are blocking the entire thread. Up to the library user to run the client in a
  309.     963 # different thread if necessary.
  310.     964 time.sleep(timeout)
  311. --> 966 return self._request(
  312.     967     options=options,
  313.     968     cast_to=cast_to,
  314.     969     remaining_retries=remaining,
  315.     970     stream=stream,
  316.     971     stream_cls=stream_cls,
  317.     972 )
  318.  
  319. File /lib/python3.11/site-packages/openai/_base_client.py:938, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
  320.     928     if retries > 0:
  321.     929         return self._retry_request(
  322.     930             options,
  323.     931             cast_to,
  324.    (...)
  325.     935             response_headers=response.headers if response is not None else None,
  326.     936         )
  327. --> 938     raise APIConnectionError(request=request) from err
  328.     940 return self._process_response(
  329.     941     cast_to=cast_to,
  330.     942     options=options,
  331.    (...)
  332.     945     stream_cls=stream_cls,
  333.     946 )
  334.  
  335. APIConnectionError: Connection error.
  336.  
Advertisement
Add Comment
Please, Sign In to add comment