# Custom Action invalid json response from server

**URL:** <https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575>\
**Category:** Rasa Open Source\
**Created:** [April 18, 2021, 10:43pm UTC](https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575 "2021-04-18T22:43:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bqwerz](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@bqwerz](https://forum.rasa.com/u/bqwerz)\
**Post date:** [April 18, 2021, 10:43pm UTC](https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575/1 "2021-04-18T22:43:13Z")

</div>

I’m writing a custom action server in another language using API spec as a guide (not the Python SDK).

I am able to get Rasa to make a call to my server, however, when I generate a valid JSON response to send back to Rasa, the jsonschema validator within Rasa is failing. Here is the log:

```auto
raise error
jsonschema.exceptions.ValidationError: None is not of type 'object'. Failed to validate Action server response from API, make sure your response from the Action endpoint is valid. For more information about the format visit https://rasa.com/docs/rasa/custom-actions

Failed validating 'type' in schema:
    {'properties': {'events': {'items': {'properties': {'event': {'type': 'string'}},
                                         'type': 'object'},
                               'type': 'array'},
                    'responses': {'items': {'type': 'object'},
                                  'type': 'array'}},
     'type': 'object'}

On instance:
    None

```

The response sent back is very basic. It looks like this, which is according to the API spec ([Rasa Action Server Documentation](https://rasa.com/docs/action-server/pages/action-server-api)):

```auto
{
  "responses": [
    {
      "text": "Hello from server"
    }
  ]
}

```

Any help would be appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![mloubser](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mloubser/32/13021_2.png) [@mloubser](https://forum.rasa.com/u/mloubser)\
**Post date:** [April 19, 2021, 9:30pm UTC](https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575/2 "2021-04-19T21:30:55Z")

</div>

Just took a quick look here, I may be off, but I think you need to specify `events`, even when they’re empty e.g.

```auto
{
  "events": [],
  "responses": [
    {
      "text": "Hello from server"
    }
  ]
}

```

this is the method that’s validating this:

```auto

    @staticmethod
    def action_response_format_spec() -> Dict[Text, Any]:
        """Expected response schema for an Action endpoint.

        Used for validation of the response returned from the
        Action endpoint."""
        return {
            "type": "object",
            "properties": {
                "events": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {"event": {"type": "string"}},
                    },
                },
                "responses": {"type": "array", "items": {"type": "object"}},
            },
        }

```

---

<div class="post-metadata">

**Author:** ![bqwerz](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@bqwerz](https://forum.rasa.com/u/bqwerz)\
**Post date:** [April 20, 2021, 6:44am UTC](https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575/3 "2021-04-20T06:44:15Z")

</div>

@mloubser I’ve just tried this, the same result. I also tried several other combinations of the response:

```auto
{
  "events": [{}],
  "responses": [
    {
      "text": "Hello from server"
    }
  ]
}

```

and also this, even though action should not be explicitly set ([Events](https://rasa.com/docs/action-server/events#action)):

```auto
{
  "events": [
    {
      "event": "action",
      "name": "action_test"
    }
  ],
  "responses": [
    {
      "text": "Hello from server"
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![mloubser](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mloubser/32/13021_2.png) [@mloubser](https://forum.rasa.com/u/mloubser)\
**Post date:** [April 20, 2021, 9:52am UTC](https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575/4 "2021-04-20T09:52:31Z")

</div>

Could you post the code that is sending this response? I know you said it’s not python, that’s fine, but someone one here could probably help

---

<div class="post-metadata">

**Author:** ![mloubser](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mloubser/32/13021_2.png) [@mloubser](https://forum.rasa.com/u/mloubser)\
**Post date:** [April 20, 2021, 9:56am UTC](https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575/5 "2021-04-20T09:56:47Z")

</div>

I tried this to see what part of this is throwing the error:

```auto
>>> from rasa.core.actions.action import RemoteAction
>>> payload = {
... "responses": [
... {
... "text": "Hello from server"
... }
... ]
... }
>>> action = RemoteAction("hallo","http://localhost:5055")
>>> action._validate_action_result(payload)
True

```

So, the ‘result’ it is validating must not in fact be the payload you posted; is it getting processed somehow?

---

<div class="post-metadata">

**Author:** ![bqwerz](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@bqwerz](https://forum.rasa.com/u/bqwerz)\
**Post date:** [April 21, 2021, 4:47am UTC](https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575/6 "2021-04-21T04:47:05Z")

</div>

@mloubser I’m currently using Elixir code to run a Phoenix HTTP server. As seen below, I’m creating the proper structure to be returned and JSON encoding it.

Here are the outputs from my code:

```auto
Struct: %{events: [], responses: [%{text: "Hello world from server"}]}
Json string: "{\"responses\":[{\"text\":\"Hello world from server\"}],\"events\":[]}"

```

`chat_action` is the entry point of the HTTP request

```auto
  def chat_action(conn, body = _params) do
    case Rasa.handle_action(body) do
      nil ->
        send_resp(conn, 400, "error")

      result ->
        response = Poison.encode!(result)

        put_resp_header(conn, "content-type", "application/json")
        send_resp(conn, 200, response)
    end
  end

  def handle_action(message) do
    case Map.get(message, "next_action") do
      nil ->
        Logger.error("[rasa] no action")
        nil

      "action_test" ->
        %{
          responses: [construct_response("Hello world from server")],
          events: []
        }
    end
  end

  def construct_response(text) do
    %{
      text: text
    }
  end

```

---

<div class="post-metadata">

**Author:** ![mloubser](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mloubser/32/13021_2.png) [@mloubser](https://forum.rasa.com/u/mloubser)\
**Post date:** [April 21, 2021, 9:25am UTC](https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575/7 "2021-04-21T09:25:53Z")

</div>

> [@mloubser](#):
>
> `action._validate_action_result(payload)`

Ok I think I got something - look at the `on instance` message in your error. It is validating a `None` response, not your payload. This is what I get if I enter an intentionally wrong payload:

```auto
>>> payload = "wrong"
>>> action._validate_action_result(payload)
...
Failed validating 'type' in schema:
    {'properties': {'events': {'items': {'properties': {'event': {'type': 'string'}},
                                         'type': 'object'},
                               'type': 'array'},
                    'responses': {'items': {'type': 'object'},
                                  'type': 'array'}},
     'type': 'object'}

On instance:
    'wrong'

```

vs. in the original error

```auto
Failed validating 'type' in schema:
    {'properties': {'events': {'items': {'properties': {'event': {'type': 'string'}},
                                         'type': 'object'},
                               'type': 'array'},
                    'responses': {'items': {'type': 'object'},
                                  'type': 'array'}},
     'type': 'object'}

On instance:
    None

```

---

<div class="post-metadata">

**Author:** ![bqwerz](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@bqwerz](https://forum.rasa.com/u/bqwerz)\
**Post date:** [April 22, 2021, 7:23am UTC](https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575/8 "2021-04-22T07:23:13Z")

</div>

@mloubser Is it possible that rasa instance isn’t receiving my server’s response body? Do you know of any way to log the responses that rasa is receiving to confirm this?

---

<div class="post-metadata">

**Author:** ![mloubser](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mloubser/32/13021_2.png) [@mloubser](https://forum.rasa.com/u/mloubser)\
**Post date:** [April 23, 2021, 3:55pm UTC](https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575/9 "2021-04-23T15:55:00Z")

</div>

It would be useful to see the response body being sent. You could do this by adding debug statements directly in rasa’s code; easier would be to add it to your action server somewhere - I’m not familiar with Elixir, but can you get a dump of the network request sent by `send_resp`?

---

<div class="post-metadata">

**Author:** ![Cypher](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/cypher/32/14605_2.png) [@Cypher](https://forum.rasa.com/u/Cypher)\
**Post date:** [May 8, 2021, 7:30pm UTC](https://forum.rasa.com/t/custom-action-invalid-json-response-from-server/42575/10 "2021-05-08T19:30:03Z")

</div>

You can try log the response from action server

 ![rasa](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/b/b782c61033235b0ed98491a8dce861733a9bce29.png)

response should be (JSON) below format :

```
{'events': [], 'responses': [{'text': "Hey! I'm test assistant ", 'buttons': [], 'elements': [], 'custom': {}, 'template': None, 'response': None, 'image': None, 'attachment': None}]}

```
