@@ -17,15 +17,25 @@ class WenxinRerank(_CommonWenxin):
1717 def rerank (self , model : str , query : str , docs : list [str ], top_n : Optional [int ] = None ):
1818 access_token = self ._get_access_token ()
1919 url = f"{ self .api_bases [model ]} ?access_token={ access_token } "
20-
20+ # For issue #11252
21+ # for wenxin Rerank model top_n length should be equal or less than docs length
22+ if top_n is not None and top_n > len (docs ):
23+ top_n = len (docs )
24+ # for wenxin Rerank model, query should not be an empty string
25+ if query == "" :
26+ query = " " # FIXME: this is a workaround for wenxin rerank model for better user experience.
2127 try :
2228 response = httpx .post (
2329 url ,
2430 json = {"model" : model , "query" : query , "documents" : docs , "top_n" : top_n },
2531 headers = {"Content-Type" : "application/json" },
2632 )
2733 response .raise_for_status ()
28- return response .json ()
34+ data = response .json ()
35+ # wenxin error handling
36+ if "error_code" in data :
37+ raise InternalServerError (data ["error_msg" ])
38+ return data
2939 except httpx .HTTPStatusError as e :
3040 raise InternalServerError (str (e ))
3141
@@ -69,6 +79,9 @@ def _invoke(
6979 results = wenxin_rerank .rerank (model , query , docs , top_n )
7080
7181 rerank_documents = []
82+ if "results" not in results :
83+ raise ValueError ("results key not found in response" )
84+
7285 for result in results ["results" ]:
7386 index = result ["index" ]
7487 if "document" in result :
0 commit comments