forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlite_llm.py
More file actions
1095 lines (917 loc) · 32.7 KB
/
lite_llm.py
File metadata and controls
1095 lines (917 loc) · 32.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import base64
import json
import logging
import os
import re
from typing import Any
from typing import AsyncGenerator
from typing import cast
from typing import Dict
from typing import Generator
from typing import Iterable
from typing import List
from typing import Literal
from typing import Optional
from typing import Tuple
from typing import TypedDict
from typing import Union
import warnings
from google.genai import types
import litellm
from litellm import acompletion
from litellm import ChatCompletionAssistantMessage
from litellm import ChatCompletionAssistantToolCall
from litellm import ChatCompletionDeveloperMessage
from litellm import ChatCompletionMessageToolCall
from litellm import ChatCompletionToolMessage
from litellm import ChatCompletionUserMessage
from litellm import completion
from litellm import CustomStreamWrapper
from litellm import Function
from litellm import Message
from litellm import ModelResponse
from litellm import OpenAIMessageContent
from pydantic import BaseModel
from pydantic import Field
from typing_extensions import override
from .base_llm import BaseLlm
from .llm_request import LlmRequest
from .llm_response import LlmResponse
# This will add functions to prompts if functions are provided.
litellm.add_function_to_prompt = True
logger = logging.getLogger("google_adk." + __name__)
_NEW_LINE = "\n"
_EXCLUDED_PART_FIELD = {"inline_data": {"data"}}
# Mapping of LiteLLM finish_reason strings to FinishReason enum values
# Note: tool_calls/function_call map to STOP because:
# 1. FinishReason.TOOL_CALL enum does not exist (as of google-genai 0.8.0)
# 2. Tool calls represent normal completion (model stopped to invoke tools)
# 3. Gemini native responses use STOP for tool calls (see lite_llm.py:910)
_FINISH_REASON_MAPPING = {
"length": types.FinishReason.MAX_TOKENS,
"stop": types.FinishReason.STOP,
"tool_calls": (
types.FinishReason.STOP
), # Normal completion with tool invocation
"function_call": types.FinishReason.STOP, # Legacy function call variant
"content_filter": types.FinishReason.SAFETY,
}
class ChatCompletionFileUrlObject(TypedDict, total=False):
file_data: str
file_id: str
format: str
class FunctionChunk(BaseModel):
id: Optional[str]
name: Optional[str]
args: Optional[str]
index: Optional[int] = 0
class TextChunk(BaseModel):
text: str
class UsageMetadataChunk(BaseModel):
prompt_tokens: int
completion_tokens: int
total_tokens: int
cached_prompt_tokens: int = 0
class LiteLLMClient:
"""Provides acompletion method (for better testability)."""
async def acompletion(
self, model, messages, tools, **kwargs
) -> Union[ModelResponse, CustomStreamWrapper]:
"""Asynchronously calls acompletion.
Args:
model: The model name.
messages: The messages to send to the model.
tools: The tools to use for the model.
**kwargs: Additional arguments to pass to acompletion.
Returns:
The model response as a message.
"""
return await acompletion(
model=model,
messages=messages,
tools=tools,
**kwargs,
)
def completion(
self, model, messages, tools, stream=False, **kwargs
) -> Union[ModelResponse, CustomStreamWrapper]:
"""Synchronously calls completion. This is used for streaming only.
Args:
model: The model to use.
messages: The messages to send.
tools: The tools to use for the model.
stream: Whether to stream the response.
**kwargs: Additional arguments to pass to completion.
Returns:
The response from the model.
"""
return completion(
model=model,
messages=messages,
tools=tools,
stream=stream,
**kwargs,
)
def _safe_json_serialize(obj) -> str:
"""Convert any Python object to a JSON-serializable type or string.
Args:
obj: The object to serialize.
Returns:
The JSON-serialized object string or string.
"""
try:
# Try direct JSON serialization first
return json.dumps(obj, ensure_ascii=False)
except (TypeError, OverflowError):
return str(obj)
def _part_has_payload(part: types.Part) -> bool:
"""Checks whether a Part contains usable payload for the model."""
if part.text:
return True
if part.inline_data and part.inline_data.data:
return True
if part.file_data and (part.file_data.file_uri or part.file_data.data):
return True
return False
def _append_fallback_user_content_if_missing(
llm_request: LlmRequest,
) -> None:
"""Ensures there is a user message with content for LiteLLM backends.
Args:
llm_request: The request that may need a fallback user message.
"""
for content in reversed(llm_request.contents):
if content.role == "user":
parts = content.parts or []
if any(_part_has_payload(part) for part in parts):
return
if not parts:
content.parts = []
content.parts.append(
types.Part.from_text(
text="Handle the requests as specified in the System Instruction."
)
)
return
llm_request.contents.append(
types.Content(
role="user",
parts=[
types.Part.from_text(
text=(
"Handle the requests as specified in the System"
" Instruction."
)
),
],
)
)
def _extract_cached_prompt_tokens(usage: Any) -> int:
"""Extracts cached prompt tokens from LiteLLM usage.
Providers expose cached token metrics in different shapes. Common patterns:
- usage["prompt_tokens_details"]["cached_tokens"] (OpenAI/Azure style)
- usage["prompt_tokens_details"] is a list of dicts with cached_tokens
- usage["cached_prompt_tokens"] (LiteLLM-normalized for some providers)
- usage["cached_tokens"] (flat)
Args:
usage: Usage dictionary from LiteLLM response.
Returns:
Integer number of cached prompt tokens if present; otherwise 0.
"""
try:
usage_dict = usage
if hasattr(usage, "model_dump"):
usage_dict = usage.model_dump()
elif isinstance(usage, str):
try:
usage_dict = json.loads(usage)
except json.JSONDecodeError:
return 0
if not isinstance(usage_dict, dict):
return 0
details = usage_dict.get("prompt_tokens_details")
if isinstance(details, dict):
value = details.get("cached_tokens")
if isinstance(value, int):
return value
elif isinstance(details, list):
total = sum(
item.get("cached_tokens", 0)
for item in details
if isinstance(item, dict)
and isinstance(item.get("cached_tokens"), int)
)
if total > 0:
return total
for key in ("cached_prompt_tokens", "cached_tokens"):
value = usage_dict.get(key)
if isinstance(value, int):
return value
except (TypeError, AttributeError) as e:
logger.debug("Error extracting cached prompt tokens: %s", e)
return 0
def _content_to_message_param(
content: types.Content,
) -> Union[Message, list[Message]]:
"""Converts a types.Content to a litellm Message or list of Messages.
Handles multipart function responses by returning a list of
ChatCompletionToolMessage objects if multiple function_response parts exist.
Args:
content: The content to convert.
Returns:
A litellm Message, a list of litellm Messages.
"""
tool_messages = []
for part in content.parts:
if part.function_response:
tool_messages.append(
ChatCompletionToolMessage(
role="tool",
tool_call_id=part.function_response.id,
content=_safe_json_serialize(part.function_response.response),
)
)
if tool_messages:
return tool_messages if len(tool_messages) > 1 else tool_messages[0]
# Handle user or assistant messages
role = _to_litellm_role(content.role)
message_content = _get_content(content.parts) or None
if role == "user":
return ChatCompletionUserMessage(role="user", content=message_content)
else: # assistant/model
tool_calls = []
content_present = False
for part in content.parts:
if part.function_call:
tool_calls.append(
ChatCompletionAssistantToolCall(
type="function",
id=part.function_call.id,
function=Function(
name=part.function_call.name,
arguments=_safe_json_serialize(part.function_call.args),
),
)
)
elif part.text or part.inline_data:
content_present = True
final_content = message_content if content_present else None
if final_content and isinstance(final_content, list):
# when the content is a single text object, we can use it directly.
# this is needed for ollama_chat provider which fails if content is a list
final_content = (
final_content[0].get("text", "")
if final_content[0].get("type", None) == "text"
else final_content
)
return ChatCompletionAssistantMessage(
role=role,
content=final_content,
tool_calls=tool_calls or None,
)
def _get_content(
parts: Iterable[types.Part],
) -> Union[OpenAIMessageContent, str]:
"""Converts a list of parts to litellm content.
Args:
parts: The parts to convert.
Returns:
The litellm content.
"""
content_objects = []
for part in parts:
if part.text:
if len(parts) == 1:
return part.text
content_objects.append({
"type": "text",
"text": part.text,
})
elif (
part.inline_data
and part.inline_data.data
and part.inline_data.mime_type
):
base64_string = base64.b64encode(part.inline_data.data).decode("utf-8")
data_uri = f"data:{part.inline_data.mime_type};base64,{base64_string}"
# LiteLLM providers extract the MIME type from the data URI; avoid
# passing a separate `format` field that some backends reject.
if part.inline_data.mime_type.startswith("image"):
content_objects.append({
"type": "image_url",
"image_url": {"url": data_uri},
})
elif part.inline_data.mime_type.startswith("video"):
content_objects.append({
"type": "video_url",
"video_url": {"url": data_uri},
})
elif part.inline_data.mime_type.startswith("audio"):
content_objects.append({
"type": "audio_url",
"audio_url": {"url": data_uri},
})
elif part.inline_data.mime_type == "application/pdf":
content_objects.append({
"type": "file",
"file": {"file_data": data_uri},
})
else:
raise ValueError("LiteLlm(BaseLlm) does not support this content part.")
elif part.file_data and part.file_data.file_uri:
file_object: ChatCompletionFileUrlObject = {
"file_id": part.file_data.file_uri,
}
content_objects.append({
"type": "file",
"file": file_object,
})
return content_objects
def _to_litellm_role(role: Optional[str]) -> Literal["user", "assistant"]:
"""Converts a types.Content role to a litellm role.
Args:
role: The types.Content role.
Returns:
The litellm role.
"""
if role in ["model", "assistant"]:
return "assistant"
return "user"
TYPE_LABELS = {
"STRING": "string",
"NUMBER": "number",
"BOOLEAN": "boolean",
"OBJECT": "object",
"ARRAY": "array",
"INTEGER": "integer",
}
def _schema_to_dict(schema: types.Schema) -> dict:
"""Recursively converts a types.Schema to a pure-python dict
with all enum values written as lower-case strings.
Args:
schema: The schema to convert.
Returns:
The dictionary representation of the schema.
"""
# Dump without json encoding so we still get Enum members
schema_dict = schema.model_dump(exclude_none=True)
# ---- normalise this level ------------------------------------------------
if "type" in schema_dict:
# schema_dict["type"] can be an Enum or a str
t = schema_dict["type"]
schema_dict["type"] = (t.value if isinstance(t, types.Type) else t).lower()
# ---- recurse into `items` -----------------------------------------------
if "items" in schema_dict:
schema_dict["items"] = _schema_to_dict(
schema.items
if isinstance(schema.items, types.Schema)
else types.Schema.model_validate(schema_dict["items"])
)
# ---- recurse into `properties` ------------------------------------------
if "properties" in schema_dict:
new_props = {}
for key, value in schema_dict["properties"].items():
# value is a dict → rebuild a Schema object and recurse
if isinstance(value, dict):
new_props[key] = _schema_to_dict(types.Schema.model_validate(value))
# value is already a Schema instance
elif isinstance(value, types.Schema):
new_props[key] = _schema_to_dict(value)
# plain dict without nested schemas
else:
new_props[key] = value
if "type" in new_props[key]:
new_props[key]["type"] = new_props[key]["type"].lower()
schema_dict["properties"] = new_props
return schema_dict
def _function_declaration_to_tool_param(
function_declaration: types.FunctionDeclaration,
) -> dict:
"""Converts a types.FunctionDeclaration to an openapi spec dictionary.
Args:
function_declaration: The function declaration to convert.
Returns:
The openapi spec dictionary representation of the function declaration.
"""
assert function_declaration.name
properties = {}
if (
function_declaration.parameters
and function_declaration.parameters.properties
):
for key, value in function_declaration.parameters.properties.items():
properties[key] = _schema_to_dict(value)
tool_params = {
"type": "function",
"function": {
"name": function_declaration.name,
"description": function_declaration.description or "",
"parameters": {
"type": "object",
"properties": properties,
},
},
}
if (
function_declaration.parameters
and function_declaration.parameters.required
):
tool_params["function"]["parameters"][
"required"
] = function_declaration.parameters.required
return tool_params
def _model_response_to_chunk(
response: ModelResponse,
) -> Generator[
Tuple[
Optional[Union[TextChunk, FunctionChunk, UsageMetadataChunk]],
Optional[str],
],
None,
None,
]:
"""Converts a litellm message to text, function or usage metadata chunk.
Args:
response: The response from the model.
Yields:
A tuple of text or function or usage metadata chunk and finish reason.
"""
message = None
if response.get("choices", None):
message = response["choices"][0].get("message", None)
finish_reason = response["choices"][0].get("finish_reason", None)
# check streaming delta
if message is None and response["choices"][0].get("delta", None):
message = response["choices"][0]["delta"]
if message.get("content", None):
yield TextChunk(text=message.get("content")), finish_reason
if message.get("tool_calls", None):
for tool_call in message.get("tool_calls"):
# aggregate tool_call
if tool_call.type == "function":
func_name = tool_call.function.name
func_args = tool_call.function.arguments
# Ignore empty chunks that don't carry any information.
if not func_name and not func_args:
continue
yield FunctionChunk(
id=tool_call.id,
name=func_name,
args=func_args,
index=tool_call.index,
), finish_reason
if finish_reason and not (
message.get("content", None) or message.get("tool_calls", None)
):
yield None, finish_reason
if not message:
yield None, None
# Ideally usage would be expected with the last ModelResponseStream with a
# finish_reason set. But this is not the case we are observing from litellm.
# So we are sending it as a separate chunk to be set on the llm_response.
if response.get("usage", None):
yield UsageMetadataChunk(
prompt_tokens=response["usage"].get("prompt_tokens", 0),
completion_tokens=response["usage"].get("completion_tokens", 0),
total_tokens=response["usage"].get("total_tokens", 0),
cached_prompt_tokens=_extract_cached_prompt_tokens(response["usage"]),
), None
def _model_response_to_generate_content_response(
response: ModelResponse,
) -> LlmResponse:
"""Converts a litellm response to LlmResponse. Also adds usage metadata.
Args:
response: The model response.
Returns:
The LlmResponse.
"""
message = None
finish_reason = None
if (choices := response.get("choices")) and choices:
first_choice = choices[0]
message = first_choice.get("message", None)
finish_reason = first_choice.get("finish_reason", None)
if not message:
raise ValueError("No message in response")
llm_response = _message_to_generate_content_response(
message, model_version=response.model
)
if finish_reason:
# If LiteLLM already provides a FinishReason enum (e.g., for Gemini), use
# it directly. Otherwise, map the finish_reason string to the enum.
if isinstance(finish_reason, types.FinishReason):
llm_response.finish_reason = finish_reason
else:
finish_reason_str = str(finish_reason).lower()
llm_response.finish_reason = _FINISH_REASON_MAPPING.get(
finish_reason_str, types.FinishReason.OTHER
)
if response.get("usage", None):
llm_response.usage_metadata = types.GenerateContentResponseUsageMetadata(
prompt_token_count=response["usage"].get("prompt_tokens", 0),
candidates_token_count=response["usage"].get("completion_tokens", 0),
total_token_count=response["usage"].get("total_tokens", 0),
cached_content_token_count=_extract_cached_prompt_tokens(
response["usage"]
),
)
return llm_response
def _message_to_generate_content_response(
message: Message, *, is_partial: bool = False, model_version: str = None
) -> LlmResponse:
"""Converts a litellm message to LlmResponse.
Args:
message: The message to convert.
is_partial: Whether the message is partial.
model_version: The model version used to generate the response.
Returns:
The LlmResponse.
"""
parts = []
if message.get("content", None):
parts.append(types.Part.from_text(text=message.get("content")))
if message.get("tool_calls", None):
for tool_call in message.get("tool_calls"):
if tool_call.type == "function":
part = types.Part.from_function_call(
name=tool_call.function.name,
args=json.loads(tool_call.function.arguments or "{}"),
)
part.function_call.id = tool_call.id
parts.append(part)
return LlmResponse(
content=types.Content(role="model", parts=parts),
partial=is_partial,
model_version=model_version,
)
def _get_completion_inputs(
llm_request: LlmRequest,
) -> Tuple[
List[Message],
Optional[List[Dict]],
Optional[types.SchemaUnion],
Optional[Dict],
]:
"""Converts an LlmRequest to litellm inputs and extracts generation params.
Args:
llm_request: The LlmRequest to convert.
Returns:
The litellm inputs (message list, tool dictionary, response format and
generation params).
"""
# 1. Construct messages
messages: List[Message] = []
for content in llm_request.contents or []:
message_param_or_list = _content_to_message_param(content)
if isinstance(message_param_or_list, list):
messages.extend(message_param_or_list)
elif message_param_or_list: # Ensure it's not None before appending
messages.append(message_param_or_list)
if llm_request.config.system_instruction:
messages.insert(
0,
ChatCompletionDeveloperMessage(
role="developer",
content=llm_request.config.system_instruction,
),
)
# 2. Convert tool declarations
tools: Optional[List[Dict]] = None
if (
llm_request.config
and llm_request.config.tools
and llm_request.config.tools[0].function_declarations
):
tools = [
_function_declaration_to_tool_param(tool)
for tool in llm_request.config.tools[0].function_declarations
]
# 3. Handle response format
response_format: Optional[types.SchemaUnion] = None
if llm_request.config and llm_request.config.response_schema:
response_format = llm_request.config.response_schema
# 4. Extract generation parameters
generation_params: Optional[Dict] = None
if llm_request.config:
config_dict = llm_request.config.model_dump(exclude_none=True)
# Generate LiteLlm parameters here,
# Following https://docs.litellm.ai/docs/completion/input.
generation_params = {}
param_mapping = {
"max_output_tokens": "max_completion_tokens",
"stop_sequences": "stop",
}
for key in (
"temperature",
"max_output_tokens",
"top_p",
"top_k",
"stop_sequences",
"presence_penalty",
"frequency_penalty",
):
if key in config_dict:
mapped_key = param_mapping.get(key, key)
generation_params[mapped_key] = config_dict[key]
if not generation_params:
generation_params = None
return messages, tools, response_format, generation_params
def _build_function_declaration_log(
func_decl: types.FunctionDeclaration,
) -> str:
"""Builds a function declaration log.
Args:
func_decl: The function declaration to convert.
Returns:
The function declaration log.
"""
param_str = "{}"
if func_decl.parameters and func_decl.parameters.properties:
param_str = str({
k: v.model_dump(exclude_none=True)
for k, v in func_decl.parameters.properties.items()
})
return_str = "None"
if func_decl.response:
return_str = str(func_decl.response.model_dump(exclude_none=True))
return f"{func_decl.name}: {param_str} -> {return_str}"
def _build_request_log(req: LlmRequest) -> str:
"""Builds a request log.
Args:
req: The request to convert.
Returns:
The request log.
"""
function_decls: list[types.FunctionDeclaration] = cast(
list[types.FunctionDeclaration],
req.config.tools[0].function_declarations if req.config.tools else [],
)
function_logs = (
[
_build_function_declaration_log(func_decl)
for func_decl in function_decls
]
if function_decls
else []
)
contents_logs = [
content.model_dump_json(
exclude_none=True,
exclude={
"parts": {
i: _EXCLUDED_PART_FIELD for i in range(len(content.parts))
}
},
)
for content in req.contents
]
return f"""
LLM Request:
-----------------------------------------------------------
System Instruction:
{req.config.system_instruction}
-----------------------------------------------------------
Contents:
{_NEW_LINE.join(contents_logs)}
-----------------------------------------------------------
Functions:
{_NEW_LINE.join(function_logs)}
-----------------------------------------------------------
"""
def _is_litellm_gemini_model(model_string: str) -> bool:
"""Check if the model is a Gemini model accessed via LiteLLM.
Args:
model_string: A LiteLLM model string (e.g., "gemini/gemini-2.5-pro" or
"vertex_ai/gemini-2.5-flash")
Returns:
True if it's a Gemini model accessed via LiteLLM, False otherwise
"""
# Matches "gemini/gemini-*" (Google AI Studio) or "vertex_ai/gemini-*" (Vertex AI).
pattern = r"^(gemini|vertex_ai)/gemini-"
return bool(re.match(pattern, model_string))
def _extract_gemini_model_from_litellm(litellm_model: str) -> str:
"""Extract the pure Gemini model name from a LiteLLM model string.
Args:
litellm_model: LiteLLM model string like "gemini/gemini-2.5-pro"
Returns:
Pure Gemini model name like "gemini-2.5-pro"
"""
# Remove LiteLLM provider prefix
if "/" in litellm_model:
return litellm_model.split("/", 1)[1]
return litellm_model
def _warn_gemini_via_litellm(model_string: str) -> None:
"""Warn if Gemini is being used via LiteLLM.
This function logs a warning suggesting users use Gemini directly rather than
through LiteLLM for better performance and features.
Args:
model_string: The LiteLLM model string to check
"""
if not _is_litellm_gemini_model(model_string):
return
# Check if warning should be suppressed via environment variable
if os.environ.get(
"ADK_SUPPRESS_GEMINI_LITELLM_WARNINGS", ""
).strip().lower() in ("1", "true", "yes", "on"):
return
warnings.warn(
f"[GEMINI_VIA_LITELLM] {model_string}: You are using Gemini via LiteLLM."
" For better performance, reliability, and access to latest features,"
" consider using Gemini directly through ADK's native Gemini"
f" integration. Replace LiteLlm(model='{model_string}') with"
f" Gemini(model='{_extract_gemini_model_from_litellm(model_string)}')."
" Set ADK_SUPPRESS_GEMINI_LITELLM_WARNINGS=true to suppress this"
" warning.",
category=UserWarning,
stacklevel=3,
)
class LiteLlm(BaseLlm):
"""Wrapper around litellm.
This wrapper can be used with any of the models supported by litellm. The
environment variable(s) needed for authenticating with the model endpoint must
be set prior to instantiating this class.
Example usage:
```
os.environ["VERTEXAI_PROJECT"] = "your-gcp-project-id"
os.environ["VERTEXAI_LOCATION"] = "your-gcp-location"
agent = Agent(
model=LiteLlm(model="vertex_ai/claude-3-7-sonnet@20250219"),
...
)
```
Attributes:
model: The name of the LiteLlm model.
llm_client: The LLM client to use for the model.
"""
llm_client: LiteLLMClient = Field(default_factory=LiteLLMClient)
"""The LLM client to use for the model."""
_additional_args: Dict[str, Any] = None
def __init__(self, model: str, **kwargs):
"""Initializes the LiteLlm class.
Args:
model: The name of the LiteLlm model.
**kwargs: Additional arguments to pass to the litellm completion api.
"""
drop_params = kwargs.pop("drop_params", None)
super().__init__(model=model, **kwargs)
# Warn if using Gemini via LiteLLM
_warn_gemini_via_litellm(model)
self._additional_args = dict(kwargs)
# preventing generation call with llm_client
# and overriding messages, tools and stream which are managed internally
self._additional_args.pop("llm_client", None)
self._additional_args.pop("messages", None)
self._additional_args.pop("tools", None)
# public api called from runner determines to stream or not
self._additional_args.pop("stream", None)
if drop_params is not None:
self._additional_args["drop_params"] = drop_params
async def generate_content_async(
self, llm_request: LlmRequest, stream: bool = False
) -> AsyncGenerator[LlmResponse, None]:
"""Generates content asynchronously.
Args:
llm_request: LlmRequest, the request to send to the LiteLlm model.
stream: bool = False, whether to do streaming call.
Yields:
LlmResponse: The model response.
"""
self._maybe_append_user_content(llm_request)
_append_fallback_user_content_if_missing(llm_request)
logger.debug(_build_request_log(llm_request))
messages, tools, response_format, generation_params = (
_get_completion_inputs(llm_request)
)
if "functions" in self._additional_args:
# LiteLLM does not support both tools and functions together.
tools = None
completion_args = {
"model": llm_request.model or self.model,
"messages": messages,
"tools": tools,
"response_format": response_format,
}
completion_args.update(self._additional_args)
if generation_params:
completion_args.update(generation_params)
if stream:
text = ""
# Track function calls by index
function_calls = {} # index -> {name, args, id}
completion_args["stream"] = True
completion_args["stream_options"] = {"include_usage": True}
aggregated_llm_response = None
aggregated_llm_response_with_tool_call = None
usage_metadata = None
fallback_index = 0
async for part in await self.llm_client.acompletion(**completion_args):
for chunk, finish_reason in _model_response_to_chunk(part):
if isinstance(chunk, FunctionChunk):
index = chunk.index or fallback_index
if index not in function_calls:
function_calls[index] = {"name": "", "args": "", "id": None}
if chunk.name:
function_calls[index]["name"] += chunk.name
if chunk.args:
function_calls[index]["args"] += chunk.args
# check if args is completed (workaround for improper chunk