Spacy: AssertionError: Status must be a string

Created on 26 Jul 2017  Â·  4Comments  Â·  Source: explosion/spaCy

Displacy throws error while running the server and checking the dependencies in the browser on 0.0.0.0:5000.

from __future__ import unicode_literals, print_function
import spacy
from spacy import displacy

text = 'But Google is starting from behind.'

nlp = spacy.load('en_core_web_sm')
doc = nlp(text)
for word in doc:
    print(word.text, word.tag_, word.dep_)
displacy.serve(doc, style='dep')

Here is the error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/home/irfan/PycharmProjects/nightly/.night/local/lib/python2.7/site-packages/spacy/displacy/__init__.py", line 68, in app
    start_response('200 OK', [('Content-type', 'text/html; charset=utf-8')])
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 173, in start_response
    assert type(status) is StringType,"Status must be a string"
AssertionError: Status must be a string
127.0.0.1 - - [26/Jul/2017 10:15:57] "GET /favicon.ico HTTP/1.1" 500 59

Info about spaCy

  • Python version: 2.7.12
  • Platform: Linux-4.4.0-87-generic-x86_64-with-Ubuntu-16.04-xenial
  • spaCy version: 2.0.0a6
bug 🌙 nightly

All 4 comments

Hmm, interesting! Thanks for the report – haven't seen this error before. This seems to be related to wsgiref (which we used to avoid additional dependencies just for the simple displaCy server – but maybe we should re-think that decision).

It's a bit confusing, though, because the displaCy server implementation is pretty much identical to the wsgiref example app, and '200 OK' should definitely be a string... Will look into this!

Edit with possible solution: Might be a Python 2/3 issue – that status should be encoded in Python 2, and a string in Python 3. Since we have unicode literals set on the file, the string has a different type. If you have a second and want to quickly test it locally, try changing line 68 to:

start_response(b'200 OK', [('Content-type', 'text/html; charset=utf-8')]) 

(This will obviously only work on Python 2. So in spaCy, we'll use our compat module to write Python 2/3-specific rules here.)

Well, it does not solve the problem either.
it shows following error after using

start_response(b'200 OK', [('Content-type', 'text/html; charset=utf-8')]) 

Error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/home/irfan/PycharmProjects/nightly/.night/local/lib/python2.7/site-packages/spacy/displacy/__init__.py", line 68, in app
    start_response(b'200 OK', [('Content-type', 'text/html; charset=utf-8')])
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 179, in start_response
    assert type(name) is StringType,"Header names must be strings"
AssertionError: Header names must be strings
127.0.0.1 - - [31/Jul/2017 13:54:04] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/home/irfan/PycharmProjects/nightly/.night/local/lib/python2.7/site-packages/spacy/displacy/__init__.py", line 68, in app
    start_response(b'200 OK', [('Content-type', 'text/html; charset=utf-8')])
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 179, in start_response
    assert type(name) is StringType,"Header names must be strings"
AssertionError: Header names must be strings
127.0.0.1 - - [31/Jul/2017 13:54:04] "GET /favicon.ico HTTP/1.1" 500 59

I've changed the line to

start_response(b'200 OK', [(b'Content-type', b'text/html; charset=utf-8')])

Now it's working fine.

Ahhh, sorry – damn strings in Python 2/3. Thanks for getting to the bottom of this. Will be fixed!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings