use OpenAIMessageParam for OpenAI message compatibility

This commit is contained in:
Aakanksha Duggal 2025-08-13 13:45:04 -04:00
parent 172ef0c9c2
commit 279ac3bc51
2 changed files with 9 additions and 8 deletions

View file

@ -50,6 +50,7 @@ from llama_stack.apis.inference import (
CompletionMessage, CompletionMessage,
Inference, Inference,
Message, Message,
OpenAIMessageParam,
SamplingParams, SamplingParams,
StopReason, StopReason,
SystemMessage, SystemMessage,
@ -516,18 +517,13 @@ class ChatAgent(ShieldRunnerMixin):
if self.agent_config.name: if self.agent_config.name:
span.set_attribute("agent_name", self.agent_config.name) span.set_attribute("agent_name", self.agent_config.name)
# Convert messages to OpenAI format # Convert messages to OpenAI format
openai_messages = [] openai_messages: list[OpenAIMessageParam] = []
for message in input_messages: for message in input_messages:
openai_message = await convert_message_to_openai_dict_new(message) openai_message = await convert_message_to_openai_dict_new(message)
openai_messages.append(openai_message) openai_messages.append(openai_message)
# Convert tool definitions to OpenAI format # Convert tool definitions to OpenAI format
openai_tools = None openai_tools = [convert_tooldef_to_openai_tool(x) for x in (self.tool_defs or [])]
if self.tool_defs:
openai_tools = []
for tool_def in self.tool_defs:
openai_tool = convert_tooldef_to_openai_tool(tool_def)
openai_tools.append(openai_tool)
# Extract tool_choice from tool_config for OpenAI compatibility # Extract tool_choice from tool_config for OpenAI compatibility
# Note: tool_choice can only be provided when tools are also provided # Note: tool_choice can only be provided when tools are also provided

View file

@ -1267,6 +1267,11 @@ async def prepare_openai_completion_params(**params):
elif isinstance(value, dict): elif isinstance(value, dict):
new_value = {k: await _prepare_value(v) for k, v in value.items()} new_value = {k: await _prepare_value(v) for k, v in value.items()}
elif isinstance(value, BaseModel): elif isinstance(value, BaseModel):
# Special handling for OpenAIMessageParam, preserve as Pydantic objects
if hasattr(value, 'role') and hasattr(value, 'content'):
new_value = value
else:
# Other BaseModel objects get converted to dicts
new_value = value.model_dump(exclude_none=True) new_value = value.model_dump(exclude_none=True)
return new_value return new_value