@@ -275,8 +275,7 @@ def validate_openai(cls, values: Any) -> Any:
275275 """Checks if langchain_openai is installed."""
276276 if not importlib .util .find_spec ("langchain_openai" ):
277277 raise ImportError (
278- "Could not import langchain_openai package. "
279- "Please install it with `pip install langchain_openai`."
278+ "Could not import langchain_openai package. Please install it with `pip install langchain_openai`."
280279 )
281280 return values
282281
@@ -303,9 +302,7 @@ def _default_params(self) -> Dict[str, Any]:
303302 "stream" : self .streaming ,
304303 }
305304
306- def _headers (
307- self , is_async : Optional [bool ] = False , body : Optional [dict ] = None
308- ) -> Dict :
305+ def _headers (self , is_async : Optional [bool ] = False , body : Optional [dict ] = None ) -> Dict :
309306 """Construct and return the headers for a request.
310307
311308 Args:
@@ -357,17 +354,13 @@ def _generate(
357354 response = chat.invoke(messages)
358355 """ # noqa: E501
359356 if self .streaming :
360- stream_iter = self ._stream (
361- messages , stop = stop , run_manager = run_manager , ** kwargs
362- )
357+ stream_iter = self ._stream (messages , stop = stop , run_manager = run_manager , ** kwargs )
363358 return generate_from_stream (stream_iter )
364359
365360 requests_kwargs = kwargs .pop ("requests_kwargs" , {})
366361 params = self ._invocation_params (stop , ** kwargs )
367362 body = self ._construct_json_body (messages , params )
368- res = self .completion_with_retry (
369- data = body , run_manager = run_manager , ** requests_kwargs
370- )
363+ res = self .completion_with_retry (data = body , run_manager = run_manager , ** requests_kwargs )
371364 return self ._process_response (res .json ())
372365
373366 def _stream (
@@ -415,9 +408,7 @@ def _stream(
415408 params = self ._invocation_params (stop , ** kwargs )
416409 body = self ._construct_json_body (messages , params ) # request json body
417410
418- response = self .completion_with_retry (
419- data = body , run_manager = run_manager , stream = True , ** requests_kwargs
420- )
411+ response = self .completion_with_retry (data = body , run_manager = run_manager , stream = True , ** requests_kwargs )
421412 default_chunk_class = AIMessageChunk
422413 for line in self ._parse_stream (response .iter_lines ()):
423414 chunk = self ._handle_sse_line (line , default_chunk_class )
@@ -467,9 +458,7 @@ async def _agenerate(
467458
468459 """ # noqa: E501
469460 if self .streaming :
470- stream_iter = self ._astream (
471- messages , stop = stop , run_manager = run_manager , ** kwargs
472- )
461+ stream_iter = self ._astream (messages , stop = stop , run_manager = run_manager , ** kwargs )
473462 return await agenerate_from_stream (stream_iter )
474463
475464 requests_kwargs = kwargs .pop ("requests_kwargs" , {})
@@ -593,19 +582,14 @@ def with_structured_output(
593582 else JsonOutputParser ()
594583 )
595584 else :
596- raise ValueError (
597- f"Unrecognized method argument. Expected `json_mode`."
598- f"Received: `{ method } `."
599- )
585+ raise ValueError (f"Unrecognized method argument. Expected `json_mode`.Received: `{ method } `." )
600586
601587 if include_raw :
602588 parser_assign = RunnablePassthrough .assign (
603589 parsed = itemgetter ("raw" ) | output_parser , parsing_error = lambda _ : None
604590 )
605591 parser_none = RunnablePassthrough .assign (parsed = lambda _ : None )
606- parser_with_fallback = parser_assign .with_fallbacks (
607- [parser_none ], exception_key = "parsing_error"
608- )
592+ parser_with_fallback = parser_assign .with_fallbacks ([parser_none ], exception_key = "parsing_error" )
609593 return RunnableMap (raw = llm ) | parser_with_fallback
610594 else :
611595 return llm | output_parser
@@ -688,9 +672,7 @@ def _process_stream_response(
688672 if not isinstance (choice , dict ):
689673 raise TypeError ("Endpoint response is not well formed." )
690674 except (KeyError , IndexError , TypeError ) as e :
691- raise ValueError (
692- "Error while formatting response payload for chat model of type"
693- ) from e
675+ raise ValueError ("Error while formatting response payload for chat model of type" ) from e
694676
695677 chunk = _convert_delta_to_message_chunk (choice ["delta" ], default_chunk_cls )
696678 default_chunk_cls = chunk .__class__
@@ -702,9 +684,7 @@ def _process_stream_response(
702684 if usage is not None :
703685 gen_info .update ({"usage" : usage })
704686
705- return ChatGenerationChunk (
706- message = chunk , generation_info = gen_info if gen_info else None
707- )
687+ return ChatGenerationChunk (message = chunk , generation_info = gen_info if gen_info else None )
708688
709689 def _process_response (self , response_json : dict ) -> ChatResult :
710690 """Formats response in OpenAI spec.
@@ -729,9 +709,7 @@ def _process_response(self, response_json: dict) -> ChatResult:
729709 if not isinstance (choices , list ):
730710 raise TypeError ("Endpoint response is not well formed." )
731711 except (KeyError , TypeError ) as e :
732- raise ValueError (
733- "Error while formatting response payload for chat model of type"
734- ) from e
712+ raise ValueError ("Error while formatting response payload for chat model of type" ) from e
735713
736714 for choice in choices :
737715 message = _convert_dict_to_message (choice ["message" ])
0 commit comments