@@ -117,10 +117,19 @@ def _setup_litellm_caching(
117117 logger .info ("🗄️ Initialized disk caching" )
118118
119119 elif cache_type == "s3" :
120- from litellm .caching .s3_cache import S3Cache
121-
122- litellm .cache = S3Cache ()
123- logger .info ("🗄️ Initialized S3 caching" )
120+ try :
121+ from litellm .caching .s3_cache import S3Cache
122+
123+ # Some versions require positional or named 's3_bucket_name'
124+ s3_bucket_name = os .getenv ("LITELLM_S3_BUCKET" )
125+ if not s3_bucket_name :
126+ raise ValueError ("Missing LITELLM_S3_BUCKET for S3 cache" )
127+ # Use explicit arg name expected by basedpyright
128+ litellm .cache = S3Cache (s3_bucket_name = s3_bucket_name )
129+ logger .info ("🗄️ Initialized S3 caching for bucket %s" , s3_bucket_name )
130+ except Exception as e :
131+ logger .warning (f"Failed to initialize S3 cache ({ e } ); falling back to in-memory cache" )
132+ litellm .cache = Cache ()
124133
125134 except Exception as e :
126135 logger .warning (f"Failed to setup { cache_type } caching: { e } . Falling back to in-memory cache." )
@@ -147,7 +156,7 @@ def _clean_messages_for_api(self, messages: List[Dict]) -> List[Dict]:
147156 clean_messages .append (clean_msg )
148157 return clean_messages
149158
150- async def _make_llm_call (self , messages : List [Dict ] , tools : List [Dict ] ) -> Dict :
159+ async def _make_llm_call (self , messages : List [Dict [ str , Any ]] , tools : List [Dict [ str , Any ]] ) -> Dict [ str , Any ] :
151160 """
152161 Make an LLM API call with retry logic and caching.
153162
@@ -162,7 +171,7 @@ async def _make_llm_call(self, messages: List[Dict], tools: List[Dict]) -> Dict:
162171 clean_messages = self ._clean_messages_for_api (messages )
163172
164173 # Prepare request parameters
165- request_params = {
174+ request_params : Dict [ str , Any ] = {
166175 "messages" : clean_messages ,
167176 "temperature" : self .temperature ,
168177 "max_tokens" : self .max_tokens ,
@@ -188,7 +197,8 @@ async def _make_llm_call(self, messages: List[Dict], tools: List[Dict]) -> Dict:
188197 response = await acompletion (model = self .model_id , ** request_params )
189198
190199 # Log cache hit/miss for monitoring
191- cache_hit = getattr (response , "_hidden_params" , {}).get ("cache_hit" , False )
200+ hidden = getattr (response , "_hidden_params" , {})
201+ cache_hit = hidden .get ("cache_hit" , False ) if isinstance (hidden , dict ) else False
192202 if cache_hit :
193203 logger .debug (f"🎯 Cache hit for model: { self .model_id } " )
194204 else :
@@ -199,31 +209,34 @@ async def _make_llm_call(self, messages: List[Dict], tools: List[Dict]) -> Dict:
199209 "choices" : [
200210 {
201211 "message" : {
202- "role" : response .choices [0 ]. message . role ,
203- "content" : response .choices [0 ]. message . content ,
212+ "role" : getattr ( getattr ( response .choices [0 ], " message" , object ()), " role" , "assistant" ) ,
213+ "content" : getattr ( getattr ( response .choices [0 ], " message" , object ()), " content" , None ) ,
204214 "tool_calls" : (
205215 [
206216 {
207- "id" : tc . id ,
208- "type" : tc . type ,
217+ "id" : getattr ( tc , "id" , None ) ,
218+ "type" : getattr ( tc , " type" , "function" ) ,
209219 "function" : {
210- "name" : tc . function . name ,
211- "arguments" : tc . function . arguments ,
220+ "name" : getattr ( getattr ( tc , " function" , None ), " name" , "tool" ) ,
221+ "arguments" : getattr ( getattr ( tc , " function" , None ), " arguments" , "{}" ) ,
212222 },
213223 }
214- for tc in (response .choices [0 ].message .tool_calls or [])
224+ for tc in (
225+ getattr (getattr (response .choices [0 ], "message" , object ()), "tool_calls" , [])
226+ or []
227+ )
215228 ]
216- if response .choices [0 ]. message . tool_calls
229+ if getattr ( getattr ( response .choices [0 ], " message" , object ()), " tool_calls" , None )
217230 else []
218231 ),
219232 },
220- "finish_reason" : response .choices [0 ]. finish_reason ,
233+ "finish_reason" : getattr ( response .choices [0 ], " finish_reason" , None ) ,
221234 }
222235 ],
223236 "usage" : {
224- "prompt_tokens" : response . usage . prompt_tokens ,
225- "completion_tokens" : response . usage . completion_tokens ,
226- "total_tokens" : response . usage . total_tokens ,
237+ "prompt_tokens" : getattr ( getattr ( response , " usage" , {}), " prompt_tokens" , 0 ) ,
238+ "completion_tokens" : getattr ( getattr ( response , " usage" , {}), " completion_tokens" , 0 ) ,
239+ "total_tokens" : getattr ( getattr ( response , " usage" , {}), " total_tokens" , 0 ) ,
227240 },
228241 }
229242
0 commit comments