Skip to content

Support for Webhooks #186

@Radiergummi

Description

@Radiergummi

Let me start by saying yes, Webhooks aren't strictly the responsibility of this library, but! many use cases like deep research mode or fine-tuning require waiting for results from OpenAI. In these cases, using webhooks is absolutely sensible, and having first-class support in this library would make it easier to handle them in Laravel without requiring another package to handle signature verification would be a good thing.

Hence, I suggest to:

  • add a middleware to verify OpenAI webhooks,
  • add a controller, similar to how Cashier solves it, that will use above middlware to validate the signature and emit an event that users can handle,
  • add a console command to register a webhook with OpenAI, and
  • add a configuration option to enable this feature (defaulting to disabled).

This would make using webhooks a breeze; imagine you're kicking off some long-running Responses request, and instead of waiting several minutes until the response is finished (while blocking one of your worker processes the entire time), you just add the following event listener:

use OpenAI\Laravel\Events\WebhookReceived;
use OpenAI\Laravel\WebhookEventType;

class OpenAIEventListener
{
    /**
     * Handle received OpenAI webhooks.
     */
    public function handle(WebhookReceived $event): void
    {
        if ($event->type === WebhookEventType::ResponseCompleted) {
            $messsage = ConversationMessage::findByOpenAiId($event->response->id);
            $messsage->text = $event->response->outputText;
            $message->save();
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions