feat: Enable sending code_interpreter tool call history in chat messages #2419
+261
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR enhances the library's capabilities for chat completions by adding support for sending
code_interpreter
tool call information within assistant messages. Previously, the library's type definitions did not adequately support structuring messages that included past code interpreter executions (input code and their corresponding outputs, like logs or images).This change allows you to include detailed
code_interpreter
tool interactions in the message history sent to the OpenAI API, which is crucial for maintaining context in conversations involving code execution via tools.Addresses issue: [Responses API Code Interpreter, e.g., #2396]
Key Changes
New TypedDicts for Code Interpreter Tool Calls:
CodeInterpreterCallParam
to represent a code interpreter tool call, includingid
,type: "code_interpreter"
, inputcode
, and a list ofoutputs
.CodeInterpreterOutputLogParam
andCodeInterpreterOutputImageParam
(withCodeInterpreterOutputImageFileParam
) to structure the different types of outputs from code execution (text logs or images with file IDs).src/openai/types/chat/code_interpreter_tool_call_param.py
.Enhanced Tool Call Flexibility:
ChatCompletionMessageToolCallParam
(used inChatCompletionAssistantMessageParam
) has been refactored to be aUnion
ofFunctionToolCallParam
(the previous structure, now renamed for clarity) and the newCodeInterpreterCallParam
.tool_calls
list in an assistant's message to seamlessly include both function and code interpreter calls.Alignment with API Behavior:
outputs
field (rather thanresults
) was chosen for code interpreter outputs to align with observed API behavior (e.g., theinclude
parameter in the Responses API and playground examples) and existing response object structures in the library.Unit Tests:
tests/lib/chat/test_chat_completion_message_params.py
to specifically validate the construction ofChatCompletionAssistantMessageParam
withcode_interpreter
tool calls, covering both log and image outputs. These tests ensure the new type definitions are functional and correctly integrated.typing.Union
in the test file was also included.Impact
TypedDict
s, providing better static analysis and developer experience.How to Test
src/openai/types/chat/
.tests/lib/chat/test_chat_completion_message_params.py
, to confirm they pass: