Replies: 2 comments 1 reply
-
|
Hey @zby , just wanted to say thanks for raising this. We're mulling it over. It'd be helpful if you could share more of the code you're trying to write. |
Beta Was this translation helpful? Give feedback.
-
Answer for #1358: ChatCompletionMessage type compatibilitySolution: Message Type Conversion PatternYou've hit a real limitation in the SDK's type system. 1. The Root Causefrom openai.types.chat import ChatCompletionMessage
# ❌ This fails because ChatCompletionMessage only allows role='assistant'
message_dict = {
"tool_call_id": "call_abc123",
"role": "tool", # ← ValidationError!
"name": "get_weather",
"content": "72°F and sunny"
}
message = ChatCompletionMessage(**message_dict) # ❌ Pydantic validation errorWhy? class ChatCompletionMessage(BaseModel):
role: Literal["assistant"] # ← Only accepts 'assistant'
content: str | None
tool_calls: list[ChatCompletionMessageToolCall] | NoneIt's designed for API responses, not API requests. 2. Correct Type for Tool ResultsUse the proper input message types from from openai import OpenAI
from openai.types.chat import (
ChatCompletionToolMessageParam,
ChatCompletionMessageParam
)
client = OpenAI()
# ✅ Correct: Use ChatCompletionToolMessageParam for tool results
tool_result_message: ChatCompletionToolMessageParam = {
"role": "tool",
"tool_call_id": tool_call.id,
"content": function_response,
}
messages: list[ChatCompletionMessageParam] = [
{"role": "user", "content": "What's the weather?"},
assistant_message, # From previous API response
tool_result_message, # Tool result
]
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=messages
)3. Message Type HierarchyOpenAI SDK has two parallel type systems: Input Types (for
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently you cannot use
ChatCompletionMessageas a tool result message:OUTPUT:
This is for saving the tool result in chat history - like in the python example at https://platform.openai.com/docs/guides/function-calling
Using objects in the chat history seems more robust than using raw dictionaries.
Beta Was this translation helpful? Give feedback.
All reactions