One thing I recommend here is comparing the type against `basestring` which will avoid errors popping up for unicode characters. 
```
if device_id is not None and not isinstance(device_id, basestring):
    ...
```