1818from airlock .schemas .envelope import MessageEnvelope , generate_nonce
1919from airlock .schemas .identity import AgentCapability
2020
21+ try :
22+ import litellm
23+
24+ _HAS_LITELLM = True
25+ except ImportError :
26+ litellm = None # type: ignore[assignment]
27+ _HAS_LITELLM = False
28+
2129_CONTROL_CHAR_RE = re .compile (r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]" )
2230_MAX_ANSWER_LENGTH = 2000
2331
@@ -347,9 +355,11 @@ async def _generate_question(
347355
348356 prompt = _GENERATION_PROMPT .format (capabilities = cap_text )
349357
350- try :
351- import litellm
358+ if not _HAS_LITELLM :
359+ logger .info ("litellm not installed, using fallback question" )
360+ return _select_fallback_question (session_id , capabilities )
352361
362+ try :
353363 kwargs : dict [str , Any ] = {
354364 "model" : model ,
355365 "messages" : [{"role" : "user" , "content" : prompt }],
@@ -358,7 +368,7 @@ async def _generate_question(
358368 if api_base :
359369 kwargs ["api_base" ] = api_base
360370
361- response = await asyncio .wait_for (litellm .acompletion (** kwargs ), timeout = 30 )
371+ response = await asyncio .wait_for (litellm .acompletion (** kwargs ), timeout = 30 ) # type: ignore[union-attr]
362372 raw = response .choices [0 ].message .content
363373 question = (raw or "" ).strip ()
364374 if question :
@@ -547,9 +557,10 @@ async def _evaluate_with_llm(
547557 prompt_template = _EVALUATION_PROMPT_STRUCTURED if use_structured else _EVALUATION_PROMPT
548558 prompt = prompt_template .format (question = question , answer = answer )
549559
550- try :
551- import litellm
560+ if not _HAS_LITELLM :
561+ return ChallengeOutcome . AMBIGUOUS , "LLM evaluation unavailable ( litellm not installed)"
552562
563+ try :
553564 kwargs : dict [str , Any ] = {
554565 "model" : model ,
555566 "messages" : [{"role" : "user" , "content" : prompt }],
@@ -562,7 +573,7 @@ async def _evaluate_with_llm(
562573 if use_structured :
563574 kwargs ["response_format" ] = {"type" : "json_object" }
564575
565- response = await asyncio .wait_for (litellm .acompletion (** kwargs ), timeout = 30 )
576+ response = await asyncio .wait_for (litellm .acompletion (** kwargs ), timeout = 30 ) # type: ignore[union-attr]
566577 raw = response .choices [0 ].message .content
567578 content = (raw or "" ).strip ()
568579 if not content :
0 commit comments