-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathlitellm_model.py
More file actions
626 lines (550 loc) · 24.4 KB
/
litellm_model.py
File metadata and controls
626 lines (550 loc) · 24.4 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
from __future__ import annotations
import json
import time
from collections.abc import AsyncIterator
from copy import copy
from typing import Any, Literal, cast, overload
from openai.types.responses.response_usage import InputTokensDetails, OutputTokensDetails
from agents.exceptions import ModelBehaviorError
try:
import litellm
except ImportError as _e:
raise ImportError(
"`litellm` is required to use the LitellmModel. You can install it via the optional "
"dependency group: `pip install 'openai-agents[litellm]'`."
) from _e
from openai import AsyncStream, NotGiven, omit
from openai.types.chat import (
ChatCompletionChunk,
ChatCompletionMessageCustomToolCall,
ChatCompletionMessageFunctionToolCall,
ChatCompletionMessageParam,
)
from openai.types.chat.chat_completion_message import (
Annotation,
AnnotationURLCitation,
ChatCompletionMessage,
)
from openai.types.chat.chat_completion_message_function_tool_call import Function
from openai.types.responses import Response
from ... import _debug
from ...agent_output import AgentOutputSchemaBase
from ...handoffs import Handoff
from ...items import ModelResponse, TResponseInputItem, TResponseStreamEvent
from ...logger import logger
from ...model_settings import ModelSettings
from ...models.chatcmpl_converter import Converter
from ...models.chatcmpl_helpers import HEADERS, HEADERS_OVERRIDE
from ...models.chatcmpl_stream_handler import ChatCmplStreamHandler
from ...models.fake_id import FAKE_RESPONSES_ID
from ...models.interface import Model, ModelTracing
from ...models.openai_responses import Converter as OpenAIResponsesConverter
from ...tool import Tool
from ...tracing import generation_span
from ...tracing.span_data import GenerationSpanData
from ...tracing.spans import Span
from ...usage import Usage
from ...util._json import _to_dump_compatible
class InternalChatCompletionMessage(ChatCompletionMessage):
"""
An internal subclass to carry reasoning_content and thinking_blocks without modifying the original model.
""" # noqa: E501
reasoning_content: str
thinking_blocks: list[dict[str, Any]] | None = None
class LitellmModel(Model):
"""This class enables using any model via LiteLLM. LiteLLM allows you to acess OpenAPI,
Anthropic, Gemini, Mistral, and many other models.
See supported models here: [litellm models](https://docs.litellm.ai/docs/providers).
"""
def __init__(
self,
model: str,
base_url: str | None = None,
api_key: str | None = None,
):
self.model = model
self.base_url = base_url
self.api_key = api_key
async def get_response(
self,
system_instructions: str | None,
input: str | list[TResponseInputItem],
model_settings: ModelSettings,
tools: list[Tool],
output_schema: AgentOutputSchemaBase | None,
handoffs: list[Handoff],
tracing: ModelTracing,
previous_response_id: str | None = None, # unused
conversation_id: str | None = None, # unused
prompt: Any | None = None,
) -> ModelResponse:
with generation_span(
model=str(self.model),
model_config=model_settings.to_json_dict()
| {"base_url": str(self.base_url or ""), "model_impl": "litellm"},
disabled=tracing.is_disabled(),
) as span_generation:
response = await self._fetch_response(
system_instructions,
input,
model_settings,
tools,
output_schema,
handoffs,
span_generation,
tracing,
stream=False,
prompt=prompt,
)
assert isinstance(response.choices[0], litellm.types.utils.Choices)
if _debug.DONT_LOG_MODEL_DATA:
logger.debug("Received model response")
else:
logger.debug(
f"""LLM resp:\n{
json.dumps(
response.choices[0].message.model_dump(), indent=2, ensure_ascii=False
)
}\n"""
)
if hasattr(response, "usage"):
response_usage = response.usage
usage = (
Usage(
requests=1,
input_tokens=response_usage.prompt_tokens,
output_tokens=response_usage.completion_tokens,
total_tokens=response_usage.total_tokens,
input_tokens_details=InputTokensDetails(
cached_tokens=getattr(
response_usage.prompt_tokens_details, "cached_tokens", 0
)
or 0
),
output_tokens_details=OutputTokensDetails(
reasoning_tokens=getattr(
response_usage.completion_tokens_details, "reasoning_tokens", 0
)
or 0
),
)
if response.usage
else Usage()
)
else:
usage = Usage()
logger.warning("No usage information returned from Litellm")
if tracing.include_data():
span_generation.span_data.output = [response.choices[0].message.model_dump()]
span_generation.span_data.usage = {
"input_tokens": usage.input_tokens,
"output_tokens": usage.output_tokens,
}
items = Converter.message_to_output_items(
LitellmConverter.convert_message_to_openai(response.choices[0].message)
)
return ModelResponse(
output=items,
usage=usage,
response_id=None,
)
async def stream_response(
self,
system_instructions: str | None,
input: str | list[TResponseInputItem],
model_settings: ModelSettings,
tools: list[Tool],
output_schema: AgentOutputSchemaBase | None,
handoffs: list[Handoff],
tracing: ModelTracing,
previous_response_id: str | None = None, # unused
conversation_id: str | None = None, # unused
prompt: Any | None = None,
) -> AsyncIterator[TResponseStreamEvent]:
with generation_span(
model=str(self.model),
model_config=model_settings.to_json_dict()
| {"base_url": str(self.base_url or ""), "model_impl": "litellm"},
disabled=tracing.is_disabled(),
) as span_generation:
response, stream = await self._fetch_response(
system_instructions,
input,
model_settings,
tools,
output_schema,
handoffs,
span_generation,
tracing,
stream=True,
prompt=prompt,
)
final_response: Response | None = None
async for chunk in ChatCmplStreamHandler.handle_stream(response, stream):
yield chunk
if chunk.type == "response.completed":
final_response = chunk.response
if tracing.include_data() and final_response:
span_generation.span_data.output = [final_response.model_dump()]
if final_response and final_response.usage:
span_generation.span_data.usage = {
"input_tokens": final_response.usage.input_tokens,
"output_tokens": final_response.usage.output_tokens,
}
@overload
async def _fetch_response(
self,
system_instructions: str | None,
input: str | list[TResponseInputItem],
model_settings: ModelSettings,
tools: list[Tool],
output_schema: AgentOutputSchemaBase | None,
handoffs: list[Handoff],
span: Span[GenerationSpanData],
tracing: ModelTracing,
stream: Literal[True],
prompt: Any | None = None,
) -> tuple[Response, AsyncStream[ChatCompletionChunk]]: ...
@overload
async def _fetch_response(
self,
system_instructions: str | None,
input: str | list[TResponseInputItem],
model_settings: ModelSettings,
tools: list[Tool],
output_schema: AgentOutputSchemaBase | None,
handoffs: list[Handoff],
span: Span[GenerationSpanData],
tracing: ModelTracing,
stream: Literal[False],
prompt: Any | None = None,
) -> litellm.types.utils.ModelResponse: ...
async def _fetch_response(
self,
system_instructions: str | None,
input: str | list[TResponseInputItem],
model_settings: ModelSettings,
tools: list[Tool],
output_schema: AgentOutputSchemaBase | None,
handoffs: list[Handoff],
span: Span[GenerationSpanData],
tracing: ModelTracing,
stream: bool = False,
prompt: Any | None = None,
) -> litellm.types.utils.ModelResponse | tuple[Response, AsyncStream[ChatCompletionChunk]]:
# Preserve reasoning messages for tool calls when reasoning is on
# This is needed for models like Claude 4 Sonnet/Opus which support interleaved thinking
preserve_thinking_blocks = (
model_settings.reasoning is not None and model_settings.reasoning.effort is not None
)
converted_messages = Converter.items_to_messages(
input, preserve_thinking_blocks=preserve_thinking_blocks
)
# Fix for interleaved thinking bug: reorder messages to ensure tool_use comes before tool_result # noqa: E501
if "anthropic" in self.model.lower() or "claude" in self.model.lower():
converted_messages = self._fix_tool_message_ordering(converted_messages)
if system_instructions:
converted_messages.insert(
0,
{
"content": system_instructions,
"role": "system",
},
)
converted_messages = _to_dump_compatible(converted_messages)
if tracing.include_data():
span.span_data.input = converted_messages
parallel_tool_calls = (
True
if model_settings.parallel_tool_calls and tools and len(tools) > 0
else False
if model_settings.parallel_tool_calls is False
else None
)
tool_choice = Converter.convert_tool_choice(model_settings.tool_choice)
response_format = Converter.convert_response_format(output_schema)
converted_tools = [Converter.tool_to_openai(tool) for tool in tools] if tools else []
for handoff in handoffs:
converted_tools.append(Converter.convert_handoff_tool(handoff))
converted_tools = _to_dump_compatible(converted_tools)
if _debug.DONT_LOG_MODEL_DATA:
logger.debug("Calling LLM")
else:
messages_json = json.dumps(
converted_messages,
indent=2,
ensure_ascii=False,
)
tools_json = json.dumps(
converted_tools,
indent=2,
ensure_ascii=False,
)
logger.debug(
f"Calling Litellm model: {self.model}\n"
f"{messages_json}\n"
f"Tools:\n{tools_json}\n"
f"Stream: {stream}\n"
f"Tool choice: {tool_choice}\n"
f"Response format: {response_format}\n"
)
reasoning_effort = model_settings.reasoning.effort if model_settings.reasoning else None
# Enable developers to pass non-OpenAI compatible reasoning_effort data like "none"
# Priority order:
# 1. model_settings.reasoning.effort
# 2. model_settings.extra_body["reasoning_effort"]
# 3. model_settings.extra_args["reasoning_effort"]
if (
reasoning_effort is None # Unset in model_settings
and isinstance(model_settings.extra_body, dict)
and "reasoning_effort" in model_settings.extra_body
):
reasoning_effort = model_settings.extra_body["reasoning_effort"]
if (
reasoning_effort is None # Unset in both model_settings and model_settings.extra_body
and model_settings.extra_args
and "reasoning_effort" in model_settings.extra_args
):
reasoning_effort = model_settings.extra_args["reasoning_effort"]
stream_options = None
if stream and model_settings.include_usage is not None:
stream_options = {"include_usage": model_settings.include_usage}
extra_kwargs = {}
if model_settings.extra_query:
extra_kwargs["extra_query"] = copy(model_settings.extra_query)
if model_settings.metadata:
extra_kwargs["metadata"] = copy(model_settings.metadata)
if model_settings.extra_body and isinstance(model_settings.extra_body, dict):
extra_kwargs.update(model_settings.extra_body)
# Add kwargs from model_settings.extra_args, filtering out None values
if model_settings.extra_args:
extra_kwargs.update(model_settings.extra_args)
# Prevent duplicate reasoning_effort kwargs when it was promoted to a top-level argument.
extra_kwargs.pop("reasoning_effort", None)
ret = await litellm.acompletion(
model=self.model,
messages=converted_messages,
tools=converted_tools or None,
temperature=model_settings.temperature,
top_p=model_settings.top_p,
frequency_penalty=model_settings.frequency_penalty,
presence_penalty=model_settings.presence_penalty,
max_tokens=model_settings.max_tokens,
tool_choice=self._remove_not_given(tool_choice),
response_format=self._remove_not_given(response_format),
parallel_tool_calls=parallel_tool_calls,
stream=stream,
stream_options=stream_options,
reasoning_effort=reasoning_effort,
top_logprobs=model_settings.top_logprobs,
extra_headers=self._merge_headers(model_settings),
api_key=self.api_key,
base_url=self.base_url,
**extra_kwargs,
)
if isinstance(ret, litellm.types.utils.ModelResponse):
return ret
responses_tool_choice = OpenAIResponsesConverter.convert_tool_choice(
model_settings.tool_choice
)
if responses_tool_choice is None or responses_tool_choice is omit:
responses_tool_choice = "auto"
response = Response(
id=FAKE_RESPONSES_ID,
created_at=time.time(),
model=self.model,
object="response",
output=[],
tool_choice=responses_tool_choice, # type: ignore[arg-type]
top_p=model_settings.top_p,
temperature=model_settings.temperature,
tools=[],
parallel_tool_calls=parallel_tool_calls or False,
reasoning=model_settings.reasoning,
)
return response, ret
def _fix_tool_message_ordering(
self, messages: list[ChatCompletionMessageParam]
) -> list[ChatCompletionMessageParam]:
"""
Fix the ordering of tool messages to ensure tool_use messages come before tool_result messages.
This addresses the interleaved thinking bug where conversation histories may contain
tool results before their corresponding tool calls, causing Anthropic API to reject the request.
""" # noqa: E501
if not messages:
return messages
# Collect all tool calls and tool results
tool_call_messages = {} # tool_id -> (index, message)
tool_result_messages = {} # tool_id -> (index, message)
other_messages = [] # (index, message) for non-tool messages
for i, message in enumerate(messages):
if not isinstance(message, dict):
other_messages.append((i, message))
continue
role = message.get("role")
if role == "assistant" and message.get("tool_calls"):
# Extract tool calls from this assistant message
tool_calls = message.get("tool_calls", [])
if isinstance(tool_calls, list):
for tool_call in tool_calls:
if isinstance(tool_call, dict):
tool_id = tool_call.get("id")
if tool_id:
# Create a separate assistant message for each tool call
single_tool_msg = cast(dict[str, Any], message.copy())
single_tool_msg["tool_calls"] = [tool_call]
tool_call_messages[tool_id] = (
i,
cast(ChatCompletionMessageParam, single_tool_msg),
)
elif role == "tool":
tool_call_id = message.get("tool_call_id")
if tool_call_id:
tool_result_messages[tool_call_id] = (i, message)
else:
other_messages.append((i, message))
else:
other_messages.append((i, message))
# First, identify which tool results will be paired to avoid duplicates
paired_tool_result_indices = set()
for tool_id in tool_call_messages:
if tool_id in tool_result_messages:
tool_result_idx, _ = tool_result_messages[tool_id]
paired_tool_result_indices.add(tool_result_idx)
# Create the fixed message sequence
fixed_messages: list[ChatCompletionMessageParam] = []
used_indices = set()
# Add messages in their original order, but ensure tool_use → tool_result pairing
for i, original_message in enumerate(messages):
if i in used_indices:
continue
if not isinstance(original_message, dict):
fixed_messages.append(original_message)
used_indices.add(i)
continue
role = original_message.get("role")
if role == "assistant" and original_message.get("tool_calls"):
# Process each tool call in this assistant message
tool_calls = original_message.get("tool_calls", [])
if isinstance(tool_calls, list):
for tool_call in tool_calls:
if isinstance(tool_call, dict):
tool_id = tool_call.get("id")
if (
tool_id
and tool_id in tool_call_messages
and tool_id in tool_result_messages
):
# Add tool_use → tool_result pair
_, tool_call_msg = tool_call_messages[tool_id]
tool_result_idx, tool_result_msg = tool_result_messages[tool_id]
fixed_messages.append(tool_call_msg)
fixed_messages.append(tool_result_msg)
# Mark both as used
used_indices.add(tool_call_messages[tool_id][0])
used_indices.add(tool_result_idx)
elif tool_id and tool_id in tool_call_messages:
# Tool call without result - add just the tool call
_, tool_call_msg = tool_call_messages[tool_id]
fixed_messages.append(tool_call_msg)
used_indices.add(tool_call_messages[tool_id][0])
used_indices.add(i) # Mark original multi-tool message as used
elif role == "tool":
# Only preserve unmatched tool results to avoid duplicates
if i not in paired_tool_result_indices:
fixed_messages.append(original_message)
used_indices.add(i)
else:
# Regular message - add it normally
fixed_messages.append(original_message)
used_indices.add(i)
return fixed_messages
def _remove_not_given(self, value: Any) -> Any:
if value is omit or isinstance(value, NotGiven):
return None
return value
def _merge_headers(self, model_settings: ModelSettings):
return {**HEADERS, **(model_settings.extra_headers or {}), **(HEADERS_OVERRIDE.get() or {})}
class LitellmConverter:
@classmethod
def convert_message_to_openai(
cls, message: litellm.types.utils.Message
) -> ChatCompletionMessage:
if message.role != "assistant":
raise ModelBehaviorError(f"Unsupported role: {message.role}")
tool_calls: (
list[ChatCompletionMessageFunctionToolCall | ChatCompletionMessageCustomToolCall] | None
) = (
[LitellmConverter.convert_tool_call_to_openai(tool) for tool in message.tool_calls]
if message.tool_calls
else None
)
provider_specific_fields = message.get("provider_specific_fields", None)
refusal = (
provider_specific_fields.get("refusal", None) if provider_specific_fields else None
)
reasoning_content = ""
if hasattr(message, "reasoning_content") and message.reasoning_content:
reasoning_content = message.reasoning_content
# Extract full thinking blocks including signatures (for Anthropic)
thinking_blocks: list[dict[str, Any]] | None = None
if hasattr(message, "thinking_blocks") and message.thinking_blocks:
# Convert thinking blocks to dict format for compatibility
thinking_blocks = []
for block in message.thinking_blocks:
if isinstance(block, dict):
thinking_blocks.append(cast(dict[str, Any], block))
else:
# Convert object to dict by accessing its attributes
block_dict: dict[str, Any] = {}
if hasattr(block, "__dict__"):
block_dict = dict(block.__dict__.items())
elif hasattr(block, "model_dump"):
block_dict = block.model_dump()
else:
# Last resort: convert to string representation
block_dict = {"thinking": str(block)}
thinking_blocks.append(block_dict)
return InternalChatCompletionMessage(
content=message.content,
refusal=refusal,
role="assistant",
annotations=cls.convert_annotations_to_openai(message),
audio=message.get("audio", None), # litellm deletes audio if not present
tool_calls=tool_calls,
reasoning_content=reasoning_content,
thinking_blocks=thinking_blocks,
)
@classmethod
def convert_annotations_to_openai(
cls, message: litellm.types.utils.Message
) -> list[Annotation] | None:
annotations: list[litellm.types.llms.openai.ChatCompletionAnnotation] | None = message.get(
"annotations", None
)
if not annotations:
return None
return [
Annotation(
type="url_citation",
url_citation=AnnotationURLCitation(
start_index=annotation["url_citation"]["start_index"],
end_index=annotation["url_citation"]["end_index"],
url=annotation["url_citation"]["url"],
title=annotation["url_citation"]["title"],
),
)
for annotation in annotations
]
@classmethod
def convert_tool_call_to_openai(
cls, tool_call: litellm.types.utils.ChatCompletionMessageToolCall
) -> ChatCompletionMessageFunctionToolCall:
return ChatCompletionMessageFunctionToolCall(
id=tool_call.id,
type="function",
function=Function(
name=tool_call.function.name or "",
arguments=tool_call.function.arguments,
),
)