Re: [PR] feat(ai): add OpenAI Responses API (/v1/responses) support [apisix]

2026-04-09 Thread via GitHub


nic-6443 merged PR #13186:
URL: https://github.com/apache/apisix/pull/13186


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] feat(ai): add OpenAI Responses API (/v1/responses) support [apisix]

2026-04-09 Thread via GitHub


nic-6443 commented on code in PR #13186:
URL: https://github.com/apache/apisix/pull/13186#discussion_r3056379140


##
apisix/plugins/ai-protocols/init.lua:
##
@@ -43,7 +45,7 @@ local detection_order = {
 --- Detect the client protocol by asking each protocol if it matches.
 -- @param body table The parsed request body
 -- @param ctx table The request context
--- @return string Protocol name: "openai-chat" | "openai-embeddings" | 
"anthropic-messages"
+-- @return string Protocol name: "openai-chat" | "openai-responses" | 
"anthropic-messages"

Review Comment:
   Good catch! Fixed in the latest commit (1ceee59) — the `@return` doc comment 
now includes all four protocols: `openai-chat | openai-responses | 
openai-embeddings | anthropic-messages`.



##
apisix/plugins/ai-protocols/openai-responses.lua:
##
@@ -0,0 +1,315 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+--- OpenAI Responses API protocol adapter.
+-- Handles the Responses API format with its different SSE event model
+-- and response structure (output[] instead of choices[]).
+
+local core = require("apisix.core")
+local uuid = require("resty.jit-uuid")
+local sse = require("apisix.plugins.ai-transport.sse")
+local type = type
+local ipairs = ipairs
+local table = table
+local string_sub = string.sub
+
+local _M = {}
+
+
+--- Detect whether the request matches OpenAI Responses API format.
+-- Requires URI suffix (/v1/responses) and body (has input field).
+function _M.matches(body, ctx)
+local uri = ctx.var and ctx.var.uri
+return uri and string_sub(uri, -13) == "/v1/responses"
+and type(body) == "table" and body.input ~= nil
+end
+
+
+--- Check whether the request is a streaming request.
+function _M.is_streaming(body)
+return body.stream == true
+end
+
+
+
+function _M.parse_sse_event(event, ctx, state)
+if event.type == "response.output_text.delta" then
+local data, err = core.json.decode(event.data)
+if not data then
+core.log.warn("failed to decode SSE data: ", err)
+return { type = "skip" }
+end
+if type(data.delta) == "string" then
+return {
+type = "delta",
+texts = { data.delta },
+}
+end
+return { type = "skip" }
+
+elseif event.type == "response.completed" then
+local result = { type = "done" }
+local data, err = core.json.decode(event.data)
+if not data then
+core.log.warn("failed to decode response.completed SSE data: ", 
err)
+return result
+end
+if type(data.response) == "table"
+and type(data.response.usage) == "table" then
+local usage = data.response.usage
+result.type = "usage_and_done"
+result.usage = {
+prompt_tokens = usage.input_tokens or 0,
+completion_tokens = usage.output_tokens or 0,
+total_tokens = usage.total_tokens or 0,
+}
+result.raw_usage = usage
+end
+return result
+
+elseif event.type == "response.failed"
+or event.type == "response.incomplete"
+or event.type == "error" then
+return { type = "done" }
+end
+
+-- All other Responses API events are silently passed through
+return { type = "skip" }
+end
+
+
+function _M.extract_response_text(res_body)
+if type(res_body.output) ~= "table" then
+return nil
+end
+local texts = {}
+for _, item in ipairs(res_body.output) do
+if type(item) == "table" and item.type == "message"
+and type(item.content) == "table" then
+for _, part in ipairs(item.content) do
+if part.type == "output_text" and type(part.text) == "string" 
then
+core.table.insert(texts, part.text)
+end
+end
+end
+end
+if #texts > 0 then
+return table.concat(texts, " ")
+end
+return nil
+end
+
+
+--- Build a non-streaming request from system prompt and user content.
+function _M.build_simple_request(system_prompt, user_content, opts)
+local body = {
+in

Re: [PR] feat(ai): add OpenAI Responses API (/v1/responses) support [apisix]

2026-04-09 Thread via GitHub


nic-6443 commented on code in PR #13186:
URL: https://github.com/apache/apisix/pull/13186#discussion_r3056377964


##
apisix/plugins/ai-protocols/openai-responses.lua:
##
@@ -0,0 +1,315 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+--- OpenAI Responses API protocol adapter.
+-- Handles the Responses API format with its different SSE event model
+-- and response structure (output[] instead of choices[]).
+
+local core = require("apisix.core")
+local uuid = require("resty.jit-uuid")
+local sse = require("apisix.plugins.ai-transport.sse")
+local type = type
+local ipairs = ipairs
+local table = table
+local string_sub = string.sub
+
+local _M = {}
+
+
+--- Detect whether the request matches OpenAI Responses API format.
+-- Requires URI suffix (/v1/responses) and body (has input field).
+function _M.matches(body, ctx)
+local uri = ctx.var and ctx.var.uri
+return uri and string_sub(uri, -13) == "/v1/responses"
+and type(body) == "table" and body.input ~= nil
+end
+
+
+--- Check whether the request is a streaming request.
+function _M.is_streaming(body)
+return body.stream == true
+end
+
+
+
+function _M.parse_sse_event(event, ctx, state)
+if event.type == "response.output_text.delta" then
+local data, err = core.json.decode(event.data)
+if not data then
+core.log.warn("failed to decode SSE data: ", err)
+return { type = "skip" }
+end
+if type(data.delta) == "string" then
+return {
+type = "delta",
+texts = { data.delta },
+}
+end
+return { type = "skip" }
+
+elseif event.type == "response.completed" then
+local result = { type = "done" }
+local data, err = core.json.decode(event.data)
+if not data then
+core.log.warn("failed to decode response.completed SSE data: ", 
err)
+return result
+end
+if type(data.response) == "table"
+and type(data.response.usage) == "table" then
+local usage = data.response.usage
+result.type = "usage_and_done"
+result.usage = {
+prompt_tokens = usage.input_tokens or 0,
+completion_tokens = usage.output_tokens or 0,
+total_tokens = usage.total_tokens or 0,
+}
+result.raw_usage = usage
+end
+return result
+
+elseif event.type == "response.failed"
+or event.type == "response.incomplete"
+or event.type == "error" then
+return { type = "done" }
+end
+
+-- All other Responses API events are silently passed through
+return { type = "skip" }
+end
+
+
+function _M.extract_response_text(res_body)
+if type(res_body.output) ~= "table" then
+return nil
+end
+local texts = {}
+for _, item in ipairs(res_body.output) do
+if type(item) == "table" and item.type == "message"
+and type(item.content) == "table" then
+for _, part in ipairs(item.content) do
+if part.type == "output_text" and type(part.text) == "string" 
then
+core.table.insert(texts, part.text)
+end
+end
+end
+end
+if #texts > 0 then
+return table.concat(texts, " ")
+end
+return nil
+end
+
+
+--- Build a non-streaming request from system prompt and user content.
+function _M.build_simple_request(system_prompt, user_content, opts)
+local body = {
+input = user_content,
+stream = false,
+}
+if system_prompt then
+body.instructions = system_prompt
+end
+if opts and opts.model then
+body.model = opts.model
+end
+return body
+end
+
+
+function _M.extract_usage(res_body)
+if type(res_body) ~= "table" or type(res_body.usage) ~= "table" then
+return nil, nil
+end
+local raw = res_body.usage
+-- Responses API uses input_tokens / output_tokens
+local prompt = raw.input_tokens or 0
+local completion = raw.output_tokens or 0
+return {
+prompt_tokens = prompt,
+completion_tokens = completion,
+total_to

Re: [PR] feat(ai): add OpenAI Responses API (/v1/responses) support [apisix]

2026-04-09 Thread via GitHub


nic-6443 commented on code in PR #13186:
URL: https://github.com/apache/apisix/pull/13186#discussion_r3056376829


##
apisix/plugins/ai-protocols/openai-responses.lua:
##
@@ -0,0 +1,315 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+--- OpenAI Responses API protocol adapter.
+-- Handles the Responses API format with its different SSE event model
+-- and response structure (output[] instead of choices[]).
+
+local core = require("apisix.core")
+local uuid = require("resty.jit-uuid")
+local sse = require("apisix.plugins.ai-transport.sse")
+local type = type
+local ipairs = ipairs
+local table = table
+local string_sub = string.sub
+
+local _M = {}
+
+
+--- Detect whether the request matches OpenAI Responses API format.
+-- Requires URI suffix (/v1/responses) and body (has input field).
+function _M.matches(body, ctx)
+local uri = ctx.var and ctx.var.uri
+return uri and string_sub(uri, -13) == "/v1/responses"
+and type(body) == "table" and body.input ~= nil
+end
+
+
+--- Check whether the request is a streaming request.
+function _M.is_streaming(body)
+return body.stream == true
+end
+
+
+
+function _M.parse_sse_event(event, ctx, state)
+if event.type == "response.output_text.delta" then
+local data, err = core.json.decode(event.data)
+if not data then
+core.log.warn("failed to decode SSE data: ", err)
+return { type = "skip" }
+end
+if type(data.delta) == "string" then
+return {
+type = "delta",
+texts = { data.delta },
+}
+end
+return { type = "skip" }
+
+elseif event.type == "response.completed" then
+local result = { type = "done" }
+local data, err = core.json.decode(event.data)
+if not data then
+core.log.warn("failed to decode response.completed SSE data: ", 
err)
+return result
+end
+if type(data.response) == "table"
+and type(data.response.usage) == "table" then
+local usage = data.response.usage
+result.type = "usage_and_done"
+result.usage = {
+prompt_tokens = usage.input_tokens or 0,
+completion_tokens = usage.output_tokens or 0,
+total_tokens = usage.total_tokens or 0,
+}
+result.raw_usage = usage
+end
+return result
+
+elseif event.type == "response.failed"
+or event.type == "response.incomplete"
+or event.type == "error" then
+return { type = "done" }
+end
+
+-- All other Responses API events are silently passed through
+return { type = "skip" }
+end
+
+
+function _M.extract_response_text(res_body)
+if type(res_body.output) ~= "table" then
+return nil
+end
+local texts = {}
+for _, item in ipairs(res_body.output) do
+if type(item) == "table" and item.type == "message"
+and type(item.content) == "table" then
+for _, part in ipairs(item.content) do
+if part.type == "output_text" and type(part.text) == "string" 
then
+core.table.insert(texts, part.text)
+end
+end
+end
+end
+if #texts > 0 then
+return table.concat(texts, " ")
+end
+return nil
+end
+
+
+--- Build a non-streaming request from system prompt and user content.
+function _M.build_simple_request(system_prompt, user_content, opts)
+local body = {
+input = user_content,
+stream = false,
+}
+if system_prompt then
+body.instructions = system_prompt
+end
+if opts and opts.model then
+body.model = opts.model
+end
+return body
+end
+
+
+function _M.extract_usage(res_body)
+if type(res_body) ~= "table" or type(res_body.usage) ~= "table" then
+return nil, nil
+end
+local raw = res_body.usage
+-- Responses API uses input_tokens / output_tokens
+local prompt = raw.input_tokens or 0
+local completion = raw.output_tokens or 0
+return {
+prompt_tokens = prompt,
+completion_tokens = completion,
+total_to

Re: [PR] feat(ai): add OpenAI Responses API (/v1/responses) support [apisix]

2026-04-09 Thread via GitHub


nic-6443 commented on code in PR #13186:
URL: https://github.com/apache/apisix/pull/13186#discussion_r3056373170


##
apisix/plugins/ai-protocols/openai-responses.lua:
##
@@ -0,0 +1,315 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+--- OpenAI Responses API protocol adapter.
+-- Handles the Responses API format with its different SSE event model
+-- and response structure (output[] instead of choices[]).
+
+local core = require("apisix.core")
+local uuid = require("resty.jit-uuid")
+local sse = require("apisix.plugins.ai-transport.sse")
+local type = type
+local ipairs = ipairs
+local table = table
+local string_sub = string.sub
+
+local _M = {}
+
+
+--- Detect whether the request matches OpenAI Responses API format.
+-- Requires URI suffix (/v1/responses) and body (has input field).
+function _M.matches(body, ctx)
+local uri = ctx.var and ctx.var.uri
+return uri and string_sub(uri, -13) == "/v1/responses"
+and type(body) == "table" and body.input ~= nil
+end
+
+
+--- Check whether the request is a streaming request.
+function _M.is_streaming(body)
+return body.stream == true
+end
+
+
+
+function _M.parse_sse_event(event, ctx, state)
+if event.type == "response.output_text.delta" then
+local data, err = core.json.decode(event.data)
+if not data then
+core.log.warn("failed to decode SSE data: ", err)
+return { type = "skip" }
+end
+if type(data.delta) == "string" then
+return {
+type = "delta",
+texts = { data.delta },
+}
+end
+return { type = "skip" }
+
+elseif event.type == "response.completed" then
+local result = { type = "done" }
+local data, err = core.json.decode(event.data)
+if not data then
+core.log.warn("failed to decode response.completed SSE data: ", 
err)
+return result
+end
+if type(data.response) == "table"
+and type(data.response.usage) == "table" then
+local usage = data.response.usage
+result.type = "usage_and_done"
+result.usage = {
+prompt_tokens = usage.input_tokens or 0,
+completion_tokens = usage.output_tokens or 0,
+total_tokens = usage.total_tokens or 0,
+}
+result.raw_usage = usage
+end
+return result
+
+elseif event.type == "response.failed"
+or event.type == "response.incomplete"
+or event.type == "error" then
+return { type = "done" }
+end
+
+-- All other Responses API events are silently passed through
+return { type = "skip" }
+end
+
+
+function _M.extract_response_text(res_body)
+if type(res_body.output) ~= "table" then
+return nil
+end
+local texts = {}
+for _, item in ipairs(res_body.output) do
+if type(item) == "table" and item.type == "message"
+and type(item.content) == "table" then
+for _, part in ipairs(item.content) do
+if part.type == "output_text" and type(part.text) == "string" 
then
+core.table.insert(texts, part.text)
+end
+end
+end
+end
+if #texts > 0 then
+return table.concat(texts, " ")
+end
+return nil
+end
+
+
+--- Build a non-streaming request from system prompt and user content.
+function _M.build_simple_request(system_prompt, user_content, opts)
+local body = {
+input = user_content,
+stream = false,
+}
+if system_prompt then
+body.instructions = system_prompt
+end
+if opts and opts.model then
+body.model = opts.model
+end
+return body
+end
+
+
+function _M.extract_usage(res_body)
+if type(res_body) ~= "table" or type(res_body.usage) ~= "table" then
+return nil, nil
+end
+local raw = res_body.usage
+-- Responses API uses input_tokens / output_tokens
+local prompt = raw.input_tokens or 0
+local completion = raw.output_tokens or 0
+return {
+prompt_tokens = prompt,
+completion_tokens = completion,
+total_to

Re: [PR] feat(ai): add OpenAI Responses API (/v1/responses) support [apisix]

2026-04-09 Thread via GitHub


Copilot commented on code in PR #13186:
URL: https://github.com/apache/apisix/pull/13186#discussion_r3056325226


##
apisix/plugins/ai-protocols/openai-responses.lua:
##
@@ -0,0 +1,315 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+--- OpenAI Responses API protocol adapter.
+-- Handles the Responses API format with its different SSE event model
+-- and response structure (output[] instead of choices[]).
+
+local core = require("apisix.core")
+local uuid = require("resty.jit-uuid")
+local sse = require("apisix.plugins.ai-transport.sse")
+local type = type
+local ipairs = ipairs
+local table = table
+local string_sub = string.sub
+
+local _M = {}
+
+
+--- Detect whether the request matches OpenAI Responses API format.
+-- Requires URI suffix (/v1/responses) and body (has input field).
+function _M.matches(body, ctx)
+local uri = ctx.var and ctx.var.uri
+return uri and string_sub(uri, -13) == "/v1/responses"
+and type(body) == "table" and body.input ~= nil
+end
+
+
+--- Check whether the request is a streaming request.
+function _M.is_streaming(body)
+return body.stream == true
+end
+
+
+
+function _M.parse_sse_event(event, ctx, state)
+if event.type == "response.output_text.delta" then
+local data, err = core.json.decode(event.data)
+if not data then
+core.log.warn("failed to decode SSE data: ", err)
+return { type = "skip" }
+end
+if type(data.delta) == "string" then
+return {
+type = "delta",
+texts = { data.delta },
+}
+end
+return { type = "skip" }
+
+elseif event.type == "response.completed" then
+local result = { type = "done" }
+local data, err = core.json.decode(event.data)
+if not data then
+core.log.warn("failed to decode response.completed SSE data: ", 
err)
+return result
+end
+if type(data.response) == "table"
+and type(data.response.usage) == "table" then
+local usage = data.response.usage
+result.type = "usage_and_done"
+result.usage = {
+prompt_tokens = usage.input_tokens or 0,
+completion_tokens = usage.output_tokens or 0,
+total_tokens = usage.total_tokens or 0,
+}
+result.raw_usage = usage
+end
+return result
+
+elseif event.type == "response.failed"
+or event.type == "response.incomplete"
+or event.type == "error" then
+return { type = "done" }
+end
+
+-- All other Responses API events are silently passed through
+return { type = "skip" }
+end
+
+
+function _M.extract_response_text(res_body)
+if type(res_body.output) ~= "table" then
+return nil
+end
+local texts = {}
+for _, item in ipairs(res_body.output) do
+if type(item) == "table" and item.type == "message"
+and type(item.content) == "table" then
+for _, part in ipairs(item.content) do
+if part.type == "output_text" and type(part.text) == "string" 
then
+core.table.insert(texts, part.text)
+end
+end
+end
+end
+if #texts > 0 then
+return table.concat(texts, " ")
+end
+return nil
+end
+
+
+--- Build a non-streaming request from system prompt and user content.
+function _M.build_simple_request(system_prompt, user_content, opts)
+local body = {
+input = user_content,
+stream = false,
+}
+if system_prompt then
+body.instructions = system_prompt
+end
+if opts and opts.model then
+body.model = opts.model
+end
+return body
+end
+
+
+function _M.extract_usage(res_body)
+if type(res_body) ~= "table" or type(res_body.usage) ~= "table" then
+return nil, nil
+end
+local raw = res_body.usage
+-- Responses API uses input_tokens / output_tokens
+local prompt = raw.input_tokens or 0
+local completion = raw.output_tokens or 0
+return {
+prompt_tokens = prompt,
+completion_tokens = completion,
+total_tok