@@ -96,6 +96,64 @@ def _decode_inline_text_data(raw_bytes: bytes) -> str:
9696 return raw_bytes .decode ("latin-1" , errors = "replace" )
9797
9898
99+ def _iter_reasoning_texts (reasoning_value : Any ) -> Iterable [str ]:
100+ """Yields textual fragments from provider specific reasoning payloads."""
101+ if reasoning_value is None :
102+ return
103+
104+ if isinstance (reasoning_value , types .Content ):
105+ if not reasoning_value .parts :
106+ return
107+ for part in reasoning_value .parts :
108+ if part and part .text :
109+ yield part .text
110+ return
111+
112+ if isinstance (reasoning_value , str ):
113+ yield reasoning_value
114+ return
115+
116+ if isinstance (reasoning_value , list ):
117+ for value in reasoning_value :
118+ yield from _iter_reasoning_texts (value )
119+ return
120+
121+ if isinstance (reasoning_value , dict ):
122+ # LiteLLM currently nests “reasoning” text under a few known keys.
123+ # (Documented in https://docs.litellm.ai/docs/openai#reasoning-outputs)
124+ for key in ("text" , "content" , "reasoning" , "reasoning_content" ):
125+ text_value = reasoning_value .get (key )
126+ if isinstance (text_value , str ):
127+ yield text_value
128+ return
129+
130+ text_attr = getattr (reasoning_value , "text" , None )
131+ if isinstance (text_attr , str ):
132+ yield text_attr
133+ elif isinstance (reasoning_value , (int , float , bool )):
134+ yield str (reasoning_value )
135+
136+
137+ def _convert_reasoning_value_to_parts (reasoning_value : Any ) -> List [types .Part ]:
138+ """Converts provider reasoning payloads into Gemini thought parts."""
139+ return [
140+ types .Part (text = text , thought = True )
141+ for text in _iter_reasoning_texts (reasoning_value )
142+ if text
143+ ]
144+
145+
146+ def _extract_reasoning_value (message : Message | Dict [str , Any ]) -> Any :
147+ """Fetches the reasoning payload from a LiteLLM message or dict."""
148+ if message is None :
149+ return None
150+ if hasattr (message , "reasoning_content" ):
151+ return getattr (message , "reasoning_content" )
152+ if isinstance (message , dict ):
153+ return message .get ("reasoning_content" )
154+ return None
155+
156+
99157class ChatCompletionFileUrlObject (TypedDict , total = False ):
100158 file_data : str
101159 file_id : str
@@ -113,6 +171,10 @@ class TextChunk(BaseModel):
113171 text : str
114172
115173
174+ class ReasoningChunk (BaseModel ):
175+ parts : List [types .Part ]
176+
177+
116178class UsageMetadataChunk (BaseModel ):
117179 prompt_tokens : int
118180 completion_tokens : int
@@ -660,16 +722,13 @@ def _function_declaration_to_tool_param(
660722 },
661723 }
662724
663- # Handle required field from parameters
664725 required_fields = (
665726 getattr (function_declaration .parameters , "required" , None )
666727 if function_declaration .parameters
667728 else None
668729 )
669730 if required_fields :
670731 tool_params ["function" ]["parameters" ]["required" ] = required_fields
671- # parameters_json_schema already has required field in the json schema,
672- # no need to add it separately
673732
674733 return tool_params
675734
@@ -678,7 +737,14 @@ def _model_response_to_chunk(
678737 response : ModelResponse ,
679738) -> Generator [
680739 Tuple [
681- Optional [Union [TextChunk , FunctionChunk , UsageMetadataChunk ]],
740+ Optional [
741+ Union [
742+ TextChunk ,
743+ FunctionChunk ,
744+ UsageMetadataChunk ,
745+ ReasoningChunk ,
746+ ]
747+ ],
682748 Optional [str ],
683749 ],
684750 None ,
@@ -703,11 +769,18 @@ def _model_response_to_chunk(
703769
704770 message_content : Optional [OpenAIMessageContent ] = None
705771 tool_calls : list [ChatCompletionMessageToolCall ] = []
772+ reasoning_parts : List [types .Part ] = []
706773 if message is not None :
707774 (
708775 message_content ,
709776 tool_calls ,
710777 ) = _split_message_content_and_tool_calls (message )
778+ reasoning_value = _extract_reasoning_value (message )
779+ if reasoning_value :
780+ reasoning_parts = _convert_reasoning_value_to_parts (reasoning_value )
781+
782+ if reasoning_parts :
783+ yield ReasoningChunk (parts = reasoning_parts ), finish_reason
711784
712785 if message_content :
713786 yield TextChunk (text = message_content ), finish_reason
@@ -771,8 +844,13 @@ def _model_response_to_generate_content_response(
771844 if not message :
772845 raise ValueError ("No message in response" )
773846
847+ thought_parts = _convert_reasoning_value_to_parts (
848+ _extract_reasoning_value (message )
849+ )
774850 llm_response = _message_to_generate_content_response (
775- message , model_version = response .model
851+ message ,
852+ model_version = response .model ,
853+ thought_parts = thought_parts or None ,
776854 )
777855 if finish_reason :
778856 # If LiteLLM already provides a FinishReason enum (e.g., for Gemini), use
@@ -797,7 +875,11 @@ def _model_response_to_generate_content_response(
797875
798876
799877def _message_to_generate_content_response (
800- message : Message , * , is_partial : bool = False , model_version : str = None
878+ message : Message ,
879+ * ,
880+ is_partial : bool = False ,
881+ model_version : str = None ,
882+ thought_parts : Optional [List [types .Part ]] = None ,
801883) -> LlmResponse :
802884 """Converts a litellm message to LlmResponse.
803885
@@ -810,7 +892,13 @@ def _message_to_generate_content_response(
810892 The LlmResponse.
811893 """
812894
813- parts = []
895+ parts : List [types .Part ] = []
896+ if not thought_parts :
897+ thought_parts = _convert_reasoning_value_to_parts (
898+ _extract_reasoning_value (message )
899+ )
900+ if thought_parts :
901+ parts .extend (thought_parts )
814902 message_content , tool_calls = _split_message_content_and_tool_calls (message )
815903 if isinstance (message_content , str ) and message_content :
816904 parts .append (types .Part .from_text (text = message_content ))
@@ -972,15 +1060,9 @@ def _build_function_declaration_log(
9721060 k : v .model_dump (exclude_none = True )
9731061 for k , v in func_decl .parameters .properties .items ()
9741062 })
975- elif func_decl .parameters_json_schema :
976- param_str = str (func_decl .parameters_json_schema )
977-
9781063 return_str = "None"
9791064 if func_decl .response :
9801065 return_str = str (func_decl .response .model_dump (exclude_none = True ))
981- elif func_decl .response_json_schema :
982- return_str = str (func_decl .response_json_schema )
983-
9841066 return f"{ func_decl .name } : { param_str } -> { return_str } "
9851067
9861068
@@ -1182,6 +1264,7 @@ async def generate_content_async(
11821264
11831265 if stream :
11841266 text = ""
1267+ reasoning_parts : List [types .Part ] = []
11851268 # Track function calls by index
11861269 function_calls = {} # index -> {name, args, id}
11871270 completion_args ["stream" ] = True
@@ -1223,6 +1306,14 @@ async def generate_content_async(
12231306 is_partial = True ,
12241307 model_version = part .model ,
12251308 )
1309+ elif isinstance (chunk , ReasoningChunk ):
1310+ if chunk .parts :
1311+ reasoning_parts .extend (chunk .parts )
1312+ yield LlmResponse (
1313+ content = types .Content (role = "model" , parts = list (chunk .parts )),
1314+ partial = True ,
1315+ model_version = part .model ,
1316+ )
12261317 elif isinstance (chunk , UsageMetadataChunk ):
12271318 usage_metadata = types .GenerateContentResponseUsageMetadata (
12281319 prompt_token_count = chunk .prompt_tokens ,
@@ -1256,16 +1347,27 @@ async def generate_content_async(
12561347 tool_calls = tool_calls ,
12571348 ),
12581349 model_version = part .model ,
1350+ thought_parts = list (reasoning_parts )
1351+ if reasoning_parts
1352+ else None ,
12591353 )
12601354 )
12611355 text = ""
1356+ reasoning_parts = []
12621357 function_calls .clear ()
1263- elif finish_reason == "stop" and text :
1358+ elif finish_reason == "stop" and (text or reasoning_parts ):
1359+ message_content = text if text else None
12641360 aggregated_llm_response = _message_to_generate_content_response (
1265- ChatCompletionAssistantMessage (role = "assistant" , content = text ),
1361+ ChatCompletionAssistantMessage (
1362+ role = "assistant" , content = message_content
1363+ ),
12661364 model_version = part .model ,
1365+ thought_parts = list (reasoning_parts )
1366+ if reasoning_parts
1367+ else None ,
12671368 )
12681369 text = ""
1370+ reasoning_parts = []
12691371
12701372 # waiting until streaming ends to yield the llm_response as litellm tends
12711373 # to send chunk that contains usage_metadata after the chunk with
0 commit comments