ChatCompletions create() doesn't type-check enums as role
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 31.6k
- Forks
- 5.7k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 96
Description
Confirm this is a feature request for the Python library and not the underlying OpenAI API.
- This is a feature request for the Python library
Describe the feature or improvement you're requesting
This was discussed previously @ #911, but just opening a new issue so that it's in the issue tracker.
The Bug
When calling,
role: Literal["system", "user", "assistant"] = ...
completion = await client.chat.completions.create(
model="gpt-4",
messages={"role": role, "content": "Hi"}
)
Reason
The main issue is that it's defined as an enum of typed dicts,
ChatCompletionMessageParam = Union[
ChatCompletionSystemMessageParam,
ChatCompletionUserMessageParam,
ChatCompletionAssistantMessageParam,
ChatCompletionToolMessageParam,
ChatCompletionFunctionMessageParam,
]
Of course, for tooling & functions, we need to know exactly whether or not it's a user or assistant message. But role: str, content: str is the most common use-case, so it would be nice if system/user/assistant can all use that kind of interface when necessary.
Additional context
Potential Solution
From https://github.com/openai/openai-python/issues/911#issuecomment-2038669860,
I think a very simple solution would be to add another type to the ChatCompletionMessageParam union.
Currently, we have
ChatCompletionMessageParam = Union[
ChatCompletionSystemMessageParam,
ChatCompletionUserMessageParam,
ChatCompletionAssistantMessageParam,
ChatCompletionToolMessageParam,
ChatCompletionFunctionMessageParam,
]
If instead we had,
ChatCompletionMessageParam = Union[
ChatCompletionSystemMessageParam,
ChatCompletionUserMessageParam,
ChatCompletionAssistantMessageParam,
ChatCompletionToolMessageParam,
ChatCompletionFunctionMessageParam,
ChatCompletionGenericMessageParam,
]
Where ChatCompletionGenericMessageParam was,
class ChatCompletionGenericMessageParam(TypedDict, total=False):
content: Required[Optional[str]]
role: Required[Literal["system", "user", "assistant"]]
- Under my understanding, adding an option to a Union does not break compatibility with anybody (Lmk if I'm wrong)
- This handles all possible situations (pydantic model, custom TypedDict with Literal, a mixture of both, etc).
- And, if the user wants to pass in a tool/FunctionCall/ChatCompletionContentPartParam, then obviously they need to use the other types and prove that it's specifically a system/user/etc.
- OpenAI can still internally type check safely, because it can write a type-safe function to convert
ChatCompletionGenericMessageParaminto aUnion[ChatCompletionSystemMessageParam, ChatCompletionUserMessageParam,ChatCompletionAssistantMessageParam].
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Locate the ChatCompletionMessageParam definition and review the earlier discussion in issue #911. Use the provided example as the first type-checking case, and consider the work done when that case is accepted while the specialized system, user, assistant, tool, and function message forms remain supported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100