chore: Clean up variable names, duplication in openai_responses.py

Some small fixes to clarify variable names so that they more closely
match what they do (input_messages -> input_items) and use an
intermediate variable plus add some code comments about how we
aggregating streaming tool call arguments from the inference provider
when building our response.

Signed-off-by: Ben Browning <bbrownin@redhat.com>
This commit is contained in:
Ben Browning 2025-05-13 09:55:30 -04:00
parent 4df8caab41
commit 8064e3d412

View file

@ -98,30 +98,30 @@ async def _convert_response_input_to_chat_messages(
""" """
messages: list[OpenAIMessageParam] = [] messages: list[OpenAIMessageParam] = []
if isinstance(input, list): if isinstance(input, list):
for input_message in input: for input_item in input:
if isinstance(input_message, OpenAIResponseInputFunctionToolCallOutput): if isinstance(input_item, OpenAIResponseInputFunctionToolCallOutput):
messages.append( messages.append(
OpenAIToolMessageParam( OpenAIToolMessageParam(
content=input_message.output, content=input_item.output,
tool_call_id=input_message.call_id, tool_call_id=input_item.call_id,
) )
) )
elif isinstance(input_message, OpenAIResponseOutputMessageFunctionToolCall): elif isinstance(input_item, OpenAIResponseOutputMessageFunctionToolCall):
tool_call = OpenAIChatCompletionToolCall( tool_call = OpenAIChatCompletionToolCall(
index=0, index=0,
id=input_message.call_id, id=input_item.call_id,
function=OpenAIChatCompletionToolCallFunction( function=OpenAIChatCompletionToolCallFunction(
name=input_message.name, name=input_item.name,
arguments=input_message.arguments, arguments=input_item.arguments,
), ),
) )
messages.append(OpenAIAssistantMessageParam(tool_calls=[tool_call])) messages.append(OpenAIAssistantMessageParam(tool_calls=[tool_call]))
else: else:
content = await _convert_response_content_to_chat_content(input_message.content) content = await _convert_response_content_to_chat_content(input_item.content)
message_type = await _get_message_type_by_role(input_message.role) message_type = await _get_message_type_by_role(input_item.role)
if message_type is None: if message_type is None:
raise ValueError( raise ValueError(
f"Llama Stack OpenAI Responses does not yet support message role '{input_message.role}' in this context" f"Llama Stack OpenAI Responses does not yet support message role '{input_item.role}' in this context"
) )
messages.append(message_type(content=content)) messages.append(message_type(content=content))
else: else:
@ -257,17 +257,17 @@ class OpenAIResponsesImpl:
if chunk_choice.finish_reason: if chunk_choice.finish_reason:
chunk_finish_reason = chunk_choice.finish_reason chunk_finish_reason = chunk_choice.finish_reason
# Aggregate tool call arguments across chunks, using their index as the aggregation key
if chunk_choice.delta.tool_calls: if chunk_choice.delta.tool_calls:
for tool_call in chunk_choice.delta.tool_calls: for tool_call in chunk_choice.delta.tool_calls:
if tool_call.index not in chat_response_tool_calls: response_tool_call = chat_response_tool_calls.get(tool_call.index, None)
chat_response_tool_calls[tool_call.index] = OpenAIChatCompletionToolCall( if response_tool_call:
**tool_call.model_dump() response_tool_call.function.arguments += tool_call.function.arguments
) else:
chat_response_tool_calls[tool_call.index].function.arguments = ( response_tool_call = OpenAIChatCompletionToolCall(**tool_call.model_dump())
chat_response_tool_calls[tool_call.index].function.arguments chat_response_tool_calls[tool_call.index] = response_tool_call
+ tool_call.function.arguments
)
# Convert the dict of tool calls by index to a list of tool calls to pass back in our response
if chat_response_tool_calls: if chat_response_tool_calls:
tool_calls = [chat_response_tool_calls[i] for i in sorted(chat_response_tool_calls.keys())] tool_calls = [chat_response_tool_calls[i] for i in sorted(chat_response_tool_calls.keys())]
else: else: