Skip to content

feat(ai): switch OpenAI provider to responses API - #1457

Merged
hwbrzzl merged 11 commits into
masterfrom
openai-responses-api
May 2, 2026
Merged

feat(ai): switch OpenAI provider to responses API#1457
hwbrzzl merged 11 commits into
masterfrom
openai-responses-api

Conversation

@hwbrzzl

@hwbrzzl hwbrzzl commented May 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Switch the OpenAI text provider from Chat Completions to the Responses API for normal prompts, streaming, and tool calling.
  • Reuse provider-scoped response state so OpenAI tool loops can continue with previous_response_id and only send tool outputs on follow-up requests.
  • Inline text documents as prompt content while keeping binary documents as file attachments so OpenAI-compatible providers can handle mixed attachments more reliably.

Why

The OpenAI provider was still speaking the older Chat Completions shape, which meant Goravel could not match the Responses API behavior now expected by OpenAI-compatible backends. This updates the provider to send Responses input items, parse Responses output and stream events, and keep tool-call continuation aligned with previous_response_id so follow-up tool requests stay small and consistent.

resp, err := facades.AI().WithContext(ctx).Agent(agents.NewChatAgent())
if err != nil {
	return ctx.Response().Json(http.StatusInternalServerError, http.Json{
		"error": err.Error(),
	})
}

reply, err := resp.Prompt("What's the content of the attachments?", ai.WithAttachments(
	ai.ImageFromPath("logo.png"),
	ai.DocumentFromPath("123.txt"),
))
if err != nil {
	return ctx.Response().Json(http.StatusInternalServerError, http.Json{
		"error": err.Error(),
	})
}

return ctx.Response().Success().Json(http.Json{
	"reply": reply.Text(),
})

This also fixes two compatibility issues in real user flows. Before, tool loops had to rebuild growing request bodies instead of resuming from the prior OpenAI response, and plain-text documents such as 123.txt were uploaded as input_file, which some OpenAI-compatible providers reject unless the file is a PDF. After this change, tool continuations reuse provider state through AgentPrompt.ProviderState, and text-like documents are sent as prompt text while PDFs and other binary files continue to use file attachments.

// Before: plain text documents were always sent as file uploads.
resp, err := conv.Prompt("What's the content of the attachments?", ai.WithAttachments(
	ai.ImageFromPath("logo.png"),
	ai.DocumentFromPath("123.txt"),
))

// After: text documents are inlined as prompt text, while binary files remain file attachments.
resp, err := conv.Prompt("What's the content of the attachments?", ai.WithAttachments(
	ai.ImageFromPath("logo.png"),
	ai.DocumentFromPath("123.txt"),
	ai.DocumentFromPath("manual.pdf"),
))

Keep OpenAI-compatible tool loops aligned with the Responses API by reusing previous response state and handling attachments more reliably across providers.
Copilot AI review requested due to automatic review settings May 2, 2026 03:40
@hwbrzzl
hwbrzzl requested a review from a team as a code owner May 2, 2026 03:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR migrates the OpenAI text provider implementation from Chat Completions to the OpenAI Responses API, adding provider-scoped state to support smaller tool-loop follow-ups via previous_response_id, and improving attachment handling by inlining text-like documents as prompt text.

Changes:

  • Add ProviderState to contracts/ai.AgentPrompt and implement a conversation-scoped, thread-safe provider state map.
  • Update the OpenAI provider to build Responses API input items, parse Responses output (including tool calls), and stream via Responses SSE events.
  • Update tests to validate Responses API request bodies, tool call behavior, and previous_response_id continuation.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
contracts/ai/provider.go Adds ProviderState interface and threads it through AgentPrompt.
ai/provider_state.go Implements a thread-safe map-backed provider state used by conversations.
ai/openai/response.go Extends OpenAI response struct to track response IDs (used internally).
ai/openai/provider.go Core migration to OpenAI Responses API (prompt, stream, tool calls, attachments, continuation).
ai/openai/provider_test.go Updates prompt/stream tests to assert Responses API payloads and parsing.
ai/openai/provider_tool_test.go Updates tool tests and adds coverage for previous_response_id in prompt/stream tool loops.
ai/conversation.go Allocates and passes per-conversation ProviderState; resets state on Reset().
ai/conversation_test.go Updates expected provider calls to include ProviderState.
ai/application_test.go Updates expected prompt payloads to include ProviderState.

Comment thread ai/openai/provider.go
Comment thread ai/openai/provider.go
@codecov

codecov Bot commented May 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.88192% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.36%. Comparing base (ecfbcc1) to head (daf059a).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
ai/openai/provider.go 91.48% 10 Missing and 6 partials ⚠️
database/gorm/query.go 33.33% 4 Missing ⚠️
database/db/utils.go 66.66% 0 Missing and 1 partial ⚠️
support/collect/collection.go 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1457      +/-   ##
==========================================
+ Coverage   69.29%   69.36%   +0.06%     
==========================================
  Files         364      365       +1     
  Lines       28713    28795      +82     
==========================================
+ Hits        19897    19973      +76     
- Misses       7924     7928       +4     
- Partials      892      894       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI review requested due to automatic review settings May 2, 2026 04:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 11 comments.

Comment thread ai/openai/provider_test.go Outdated
Comment thread ai/openai/response.go Outdated
Comment thread ai/openai/provider_tool_test.go Outdated
Comment thread ai/openai/provider_tool_test.go Outdated
Comment thread ai/openai/provider_test.go Outdated
Comment thread ai/openai/provider_test.go Outdated
Comment thread ai/openai/provider_tool_test.go Outdated
Comment thread ai/openai/provider_test.go Outdated
Comment thread ai/openai/provider_tool_test.go
Comment thread ai/conversation.go
Copilot AI review requested due to automatic review settings May 2, 2026 04:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Comment thread ai/provider_state.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 2, 2026 04:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Comment thread ai/openai/provider_test.go Outdated
Comment thread ai/openai/provider_test.go Outdated
Comment thread ai/openai/provider_tool_test.go
Comment thread ai/openai/provider_tool_test.go Outdated
Copilot AI review requested due to automatic review settings May 2, 2026 05:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 22 out of 22 changed files in this pull request and generated 3 comments.

Comment thread ai/openai/provider_test.go
Comment thread ai/openai/provider_test.go Outdated
Comment thread ai/openai/provider.go Outdated
@hwbrzzl
hwbrzzl merged commit 9b66e7e into master May 2, 2026
19 checks passed
@hwbrzzl
hwbrzzl deleted the openai-responses-api branch May 2, 2026 06:46
LinboLen added a commit to LinboLen/framework that referenced this pull request May 3, 2026
* origin/master:
  fix(ai): remove context from image response content (goravel#1462)
  feat(ai): support provider-managed files (goravel#1460)
  feat(ai): add image generation support (goravel#1461)
  fix: [goravel#946] stop WithContext from leaking framework context keys (goravel#1456)
  feat(ai): add attachment upload support (goravel#1459)
  feat(ai): add attachment helper subpackages (goravel#1458)
  feat(ai): switch OpenAI provider to responses API (goravel#1457)
  feat(ai): add attachment support (goravel#1455)
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