Skip to content

feat: Add Code Interpreter support for Responses API #612

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

Conversation

msitarzewski
Copy link
Contributor

  • CreateStreamedResponse.php - Added streaming support for code interpreter
  • OutputCodeInterpreterToolCall.php - New file for code interpreter output handling
  • OutputMessageContentOutputTextAnnotationsContainerFile.php - New file for container file annotations
  • OutputMessageContentOutputText.php - Updated to support container file annotations
  • CodeInterpreterCall.php - New streaming event handler
  • OutputTextAnnotationAdded.php - New streaming event for text annotations
  • CodeInterpreterCodeDone.php - New streaming event for code completion
  • CodeInterpreterCodeDelta.php - New streaming event for code deltas
  • OutputItem.php - Updated to handle code interpreter outputs
  • CodeInterpreterTool.php - New tool type definition

What:

  • Bug Fix
  • New Feature

Description:

This PR adds comprehensive support for OpenAI's Code Interpreter tool in the Responses API, enabling the PHP client to handle code execution, file generation, and streaming events from the code interpreter.

Key additions:

  1. New Response Types:

    • OutputCodeInterpreterToolCall - Handles code interpreter execution results
    • OutputMessageContentOutputTextAnnotationsContainerFile - Processes container file citations
    • CodeInterpreterTool - Tool type definition for code interpreter
  2. Streaming Support:

    • CodeInterpreterCall - Handles code interpreter streaming events
    • CodeInterpreterCodeDelta - Streams code as it's being written
    • CodeInterpreterCodeDone - Signals code completion
    • OutputTextAnnotationAdded - Handles file annotations in streamed responses
  3. Type System Updates:

    • Added code_interpreter to tool type unions in CreateResponse
    • Added code_interpreter_call to output type unions
    • Updated OutputItem to handle code interpreter outputs
    • Enhanced OutputMessageContentOutputText to support container file annotations

Use Case Example:

$response = OpenAI::responses()->create([
    'model' => 'gpt-4o',
    'input' => [
        ['role' => 'user', 'content' => 'Create a chart showing sales data']
    ],
    'tools' => [
        ['type' => 'code_interpreter']
    ]
]);

// The response can now include code execution and generated files (charts, CSVs, etc.)

This enables PHP applications to leverage OpenAI's code interpreter for data analysis, visualization, file generation, and dynamic code execution tasks.

Testing:

- Successfully tested with streaming responses containing code interpreter outputs
- Verified container file citations are properly parsed
- Confirmed all streaming events are handled without errors
- Tested with real-world chart generation and data analysis requests

Backward Compatibility:

All changes are additive and maintain full backward compatibility with existing code.

- `CreateStreamedResponse.php` - Added streaming support for code interpreter
- `OutputCodeInterpreterToolCall.php` - New file for code interpreter output handling
- `OutputMessageContentOutputTextAnnotationsContainerFile.php` - New file for container file annotations
- `OutputMessageContentOutputText.php` - Updated to support container file annotations
- `CodeInterpreterCall.php` - New streaming event handler
- `OutputTextAnnotationAdded.php` - New streaming event for text annotations
- `CodeInterpreterCodeDone.php` - New streaming event for code completion
- `CodeInterpreterCodeDelta.php` - New streaming event for code deltas
- `OutputItem.php` - Updated to handle code interpreter outputs
- `CodeInterpreterTool.php` - New tool type definition
@iBotPeaches
Copy link
Collaborator

iBotPeaches commented Jun 21, 2025

Looks like yours is farther along than my PR - #610 - I'll close mine and review this one. Do a quick run of pint and commit the cs changes.

… code interpreter responses

composer test now reports:
• 896 PHPUnit tests PASS
• Laravel Pint style PASS
• php-stan level passes with zero errors.
@iBotPeaches
Copy link
Collaborator

I'm going to work on this merging your changes with mine (#610) as I typed the child objects, but you have completed streaming support. So merging both our work together should make this a solid completed feature.

I'll work on that today.

@iBotPeaches
Copy link
Collaborator

I merged your changes into my branch (#610) and I'll make sure to give attribution to both of us on the merge - just in my real life tests hitting some oddities.

{
  "id": "resp_6858a79d9c5081a0be80b653e7a5aece029f2a7de582cd9e",
  "object": "response",
  "created_at": 1750640541,
  "status": "completed",
  "background": false,
  "error": null,
  "incomplete_details": null,
  "instructions": "You are a personal math tutor. When asked a math question, write and run code to answer the question.",
  "max_output_tokens": null,
  "model": "gpt-4o-mini-2024-07-18",
  "output": [
    {
      "id": "ci_6858a79f85f881a0afd3c1280cf5c43f029f2a7de582cd9e",
      "type": "code_interpreter_call",
      "status": "completed",
      "code": "import sympy as sp\r\n\r\n# Define the variable\r\nx = sp.symbols('x')\r\n\r\n# Define the equation\r\nequation = sp.Eq(3*x + 11, 14)\r\n\r\n# Solve the equation\r\nsolution = sp.solve(equation, x)\r\nsolution",
      "container_id": "cntr_6858a79f206c819188fd8d3c3cf34a570bfb3170d6018bca",
      "outputs": null
    },
    {
      "id": "msg_6858a7a610d881a0a54db94e8d423e50029f2a7de582cd9e",
      "type": "message",
      "status": "completed",
      "content": [
        {
          "type": "output_text",
          "annotations": [],
          "text": "The solution to the equation \\(3x + 11 = 14\\) is \\(x = 1\\)."
        }
      ],
      "role": "assistant"
    }
  ],
  "parallel_tool_calls": true,
  "previous_response_id": null,
  "reasoning": {
    "effort": null,
    "summary": null
  },
  "service_tier": "default",
  "store": true,
  "temperature": 1,
  "text": {
    "format": {
      "type": "text"
    }
  },
  "tool_choice": "auto",
  "tools": [
    {
      "type": "code_interpreter",
      "container": {
        "type": "auto"
      }
    }
  ],
  "top_p": 1,
  "truncation": "disabled",
  "usage": {
    "input_tokens": 375,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "output_tokens": 88,
    "output_tokens_details": {
      "reasoning_tokens": 0
    },
    "total_tokens": 463
  },
  "user": null,
  "metadata": []
}

The actual payload response doesn't match docs - results vs outputs. It could be im tired and missing something so pushed my samples and will peek this tomorrow - iBotPeaches/openai-php-laravel-test@1049c2f

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.

2 participants