Skip to content

ai-proxy upstream returns error json #14903

Description

@Arjen10

Is there an existing issue for this?

  • I have searched the existing issues

Kong version ($ kong version)

kong 3.10

Current Behavior

when the ai-proxy plugin is configured with route_type = preserve, if the upstream returns an invalid json res, the request is closed unexpectedly and the client receives an empty response

Expected Behavior

kong gateway is proxy gateway, client should not receive a empty response, so return the original upstream response body to the client

Steps To Reproduce

1. configure a fake upstream API

fake upstream returns an incomplete json response

location /fake {
    content_by_lua_block {
        ngx.status = 200
        ngx.header["Content-Type"] = "application/json"
        ngx.print("{")
    }
}

fake upstream URL:

http://172.0.18.100:6443/fake

2. Configure the ai-proxy plugin

{
  "name": "ai-proxy",
  "config": {
    "route_type": "preserve",
    "llm_format": "openai",
    "model": {
      "provider": "openai",
      "options": {
        "upstream_url": "http://172.0.18.100:6443/fake"
      }
    },
    "auth": {
      "header_name": "Authorization",
      "header_value": "Bearer dummy"
    },
    "logging": {
      "log_statistics": true
    }
  }
}

3. Send a request through Kong gateway

curl --location 'http://172.18.0.21:8100/fake' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "gpt-4",
    "messages": [
      {
        "role": "user",
        "content": "hello"
      }
    ]
  }'

4. actual behavior

client receives an empty response:

curl: (52) Empty reply from server

kong logs the following error:

failed to run body_filter_by_lua*: .../kong/llm/plugin/shared-filters/serialize-analytics.lua:53:
Expected object key string but found T_END at character 2

full error log:

2026/06/09 21:18:15 [error] 2631#0: *5185 failed to run body_filter_by_lua*: ...1/kong/llm/plugin/shared-filters/serialize-analytics.lua:53: Expected object key string but found T_END at character 2
stack traceback:
        [C]: in function 'decode'
        ...1/kong/llm/plugin/shared-filters/serialize-analytics.lua:53: in function 'run'
        /usr/local/share/lua/5.1/kong/llm/plugin/base.lua:63: in function 'run_stage'
        /usr/local/share/lua/5.1/kong/llm/plugin/base.lua:135: in function </usr/local/share/lua/5.1/kong/llm/plugin/base.lua:122>
        /usr/local/share/lua/5.1/kong/init.lua:509: in function 'execute_collected_plugins_iterator'
        /usr/local/share/lua/5.1/kong/init.lua:1997: in function 'body_filter'
        body_filter_by_lua(nginx-kong.conf:142):2: in main chunk while sending to client, client: 172.18.0.21, server: kong, request: "POST /fake HTTP/1.1", host: "172.18.0.21:8100", request_id: "d82f3e71d7ca4846e0636e33d09b3f1a"

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