Skip to content

feat: Enable sending code_interpreter tool call history in chat messages #2419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Vishal-sys-code
Copy link

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

  1. New TypedDicts for Code Interpreter Tool Calls:

    • Introduced CodeInterpreterCallParam to represent a code interpreter tool call, including id, type: "code_interpreter", input code, and a list of outputs.
    • Defined CodeInterpreterOutputLogParam and CodeInterpreterOutputImageParam (with CodeInterpreterOutputImageFileParam) to structure the different types of outputs from code execution (text logs or images with file IDs).
    • These new types are located in src/openai/types/chat/code_interpreter_tool_call_param.py.
  2. Enhanced Tool Call Flexibility:

    • The existing ChatCompletionMessageToolCallParam (used in ChatCompletionAssistantMessageParam) has been refactored to be a Union of FunctionToolCallParam (the previous structure, now renamed for clarity) and the new CodeInterpreterCallParam.
    • This allows the tool_calls list in an assistant's message to seamlessly include both function and code interpreter calls.
  3. Alignment with API Behavior:

    • The outputs field (rather than results) was chosen for code interpreter outputs to align with observed API behavior (e.g., the include parameter in the Responses API and playground examples) and existing response object structures in the library.
  4. Unit Tests:

    • Added new unit tests in tests/lib/chat/test_chat_completion_message_params.py to specifically validate the construction of ChatCompletionAssistantMessageParam with code_interpreter tool calls, covering both log and image outputs. These tests ensure the new type definitions are functional and correctly integrated.
    • A minor fix to import typing.Union in the test file was also included.

Impact

  • Developers can now accurately represent past code interpreter tool executions when constructing chat histories, improving the context provided to the model.
  • Type safety is maintained, as these new structures are defined using TypedDicts, providing better static analysis and developer experience.
  • The library becomes more consistent with the evolving capabilities of the OpenAI API concerning tool use, particularly code interpreters.

How to Test

  • Review the new type definitions in src/openai/types/chat/.
  • Run the unit tests, particularly those in tests/lib/chat/test_chat_completion_message_params.py, to confirm they pass:
    pytest tests/lib/chat/test_chat_completion_message_params.py
  • Optionally, integrate these types into an application that uses assistant messages with tool calls to verify real-world usage (though API interaction itself is beyond the scope of these library changes).

@Vishal-sys-code Vishal-sys-code requested a review from a team as a code owner June 18, 2025 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant