Search code examples
pythonnlpibm-watsonnamed-entity-recognition

NLU Watson API - ApiException: Error: invalid request: content is empty, Code: 400


I am trying to do Name Entity Recognition, using Python and IBM Watson API. I did it before, but now faced a weird problem. This is a similar question.

ApiException: Error: invalid request: content is empty, Code: 400 , X-global-transaction-id: 9ba464a0-310b-4106-8044-0362ba1d5850

I managed to get results only on a very small sample (10 articles), but if I increase it, I get this error above.

My code:

# Pass credentials
authenticator = IAMAuthenticator('#######################')
natural_language_understanding = NaturalLanguageUnderstandingV1(version = '2020-08-01', authenticator = authenticator)

# Create a function for extracting information, using Nick's code
def get_company_list(text):
    response = natural_language_understanding.analyze(
           language = 'en', text = text,
           features = Features(
                        entities = EntitiesOptions(sentiment = False, emotion = False)
                        # keywords = KeywordsOptions(sentiment = True, emotion = True),
                        # concepts = ConceptsOptions(limit = 50),
                        # relations = RelationsOptions(),
                        # sentiment = SentimentOptions(),
                        # semantic_roles = SemanticRolesOptions(entities = True, keywords = True),
                        # emotion = EmotionOptions(),
                        # categories = CategoriesOptions()
            )).get_result()
  company_list = [entity['text'] for entity in response['entities'] if entity['type'] == 'Company']
  time.sleep(0.1)
  return(company_list)

# Apply the function and get a list of companies and titles
company_list_text = [get_company_list(text) for text in data1000['text']]
company_list_title = [get_company_list(text) for text in data1000['title']]

Then error occurs. I also use the university server to run it, but I am not sure it is a problem. Previously I did it successfully on Colab.


Solution

  • The ApiException: Error: invalid request: content is empty, Code: 400 error occurs when an empty string or invalid characters are passed as input to Watson NLU.

    Check your input data for missing or invalid text values before dispatching the API request.