wit-ai / wit-go

Go client for wit.ai HTTP API
MIT License
156 stars 30 forks source link

Failed to validate sample at index 0. Start must be less than end #15

Closed swys closed 4 years ago

swys commented 4 years ago

While using the ValidateSamples method with the sample code shown in the docs : https://wit.ai/docs/http/20170307#post__samples_link

[{
        "text": "I want to fly to sfo",
        "entities": [
          {
            "entity": "intent",
            "value": "flight_request"
          },
          {
            "entity": "wit$location",
            "start": 17,
            "end": 20,
            "value": "sfo"
          }
        ]
      }]

I'm getting error : unable to make a request. error: Failed to validate sample at index 0. Start must be less than end.

I also slightly altered your integration test to reproduce the same error. I added below code to the TestIntegrationSamples test :

// samples test
    _, validateErr := c.ValidateSamples([]Sample{
        {
            Text: "I want to fly SFO",
            Entities: []SampleEntity{
                {
                    Entity: "intent",
                    Value:  "flight_request",
                },
                {
                    Entity: "wit$location",
                    Value:  "SFO",
                    Start:  17,
                    End:    20,
                },
            },
        },
    })

The added lines were just to make the test match the sample curl command in the docs : https://wit.ai/docs/http/20170307#post__samples_link

And as suspected the integration test now fails with :

=== RUN   TestIntegrationSamples
--- FAIL: TestIntegrationSamples (5.53s)
    integration_test.go:197: expected nil error, got unable to make a request. error: Failed to validate sample at index 0. Start must be less than end.

I believe this is because the struct def has int values for the start and end fields, but in this example where the intent doesn't need the text selection to be called out (like when you have a trait lookup strategy) so there is no value given for start and end, they are give the zero value for the type, which is 0.

The problem is the api receives :

[
  {
    "text": "I want to fly to sfo",
    "entities": [
      {
        "entity": "intent",
        "value": "flight_request",
        "start": 0,
        "end": 0,
        "role": "",
        "subentities": null,
      },
      {
        "entity": "wit$location",
        "value": "SFO",
        "start": 17,
        "end": 20,
        "role": "",
        "subentities": null,
      }
    ]
  }
]

and it responds with a 400 :

{
  "error": "Failed to validate sample at index 0. Start must be less than end.",
  "code": "bad-request"
}

If I hit the api outside of the library and replace the values of start and end with null or omit them completely then I get back a 200 and it works as expected.

Maybe I'm missing something here? I would really appreciate some help here. Thanks!

plutov commented 4 years ago

Thanks for the feedback @swys I just made a fix in v1.0.9 version, please pull and test. I added integration test to validate that it works.

swys commented 4 years ago

@plutov thank you so much for the quick response on this, it is very appreciated. I pulled the new version and tested and it works as expected. But there is still a case where it still fails. What happens when the text that you wish to select is in the beginning of the sentence?

Example :

// samples test
    _, validateErr := c.ValidateSamples([]Sample{
        {
            Text: "SFO is where I need to go",
            Entities: []SampleEntity{
                {
                    Entity: "intent",
                    Value:  "flight_request",
                },
                {
                    Entity: "wit$location",
                    Value:  "SFO",
                    Start:  0,
                    End:    3,
                },
            },
        },
    })

In this case you would have to put the start:0 and end:3 to select the entire word, but because 0 is the zero value for the type and you enabled omitempty on the json tags, the start field will be removed from the resulting json and the integration test fails with :

--- FAIL: TestIntegrationSamples (5.40s)
    integration_test.go:197: expected nil error, got unable to make a request. error: Failed to validate sample at index 0. You must provide both start and end, or neither.
plutov commented 4 years ago

Good catch, let me reopen it then.

plutov commented 4 years ago

Removed omitempty.