Lamar Lamar - 1 year ago 144
JSON Question

API Coinbase ValueError from get_buy_price()

python 3.4 and Coinbase V2 API

I am working on some BTC data analysis and trying to make continuous requests to coinbase API. When running my script, it will always eventually crash on a calls to

r = client.get_spot_price()
r = client.get_buy_price()
r = client.get_sell_price()

The unusual thing is that the script will always crash at different times. Sometimes it will successfully collect data for an hour or so and then crash, other times it will crash after 5 - 10 minutes.


r = client.get_spot_price()
File "/home/g/.local/lib/python3.4/site-packages/coinbase/wallet/", line 191, in get_spot_price
response = self._get('v2', 'prices', 'spot', data=params)
File "/home/g/.local/lib/python3.4/site-packages/coinbase/wallet/", line 129, in _get
return self._request('get', *args, **kwargs)
File "/home/g/.local/lib/python3.4/site-packages/coinbase/wallet/", line 116, in _request
return self._handle_response(response)
File "/home/g/.local/lib/python3.4/site-packages/coinbase/wallet/", line 125, in _handle_response
raise build_api_error(response)
File "/home/g/.local/lib/python3.4/site-packages/coinbase/wallet/", line 49, in build_api_error
blob = blob or response.json()
File "/home/g/.local/lib/python3.4/site-packages/requests/", line 812, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3.4/json/", line 318, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.4/json/", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.4/json/", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

It seems to be crashing due to some json decoding?

Does anyone have any idea why this will only throw errors at certain times?

I have tried something like the following to avoid crashing due to this error:

#snap is tuple of data containing data from buy, sell , spot price
if not any(snap):
print('\n\n-----ENTRY ERROR---- Snap returned None \n\n')
success = False

but it isn't doing the trick

What are some good ways to handle this error in your opinion?

Thanks, any help is much appreciated!

Answer Source

For me it could be something related with that issue It seems in fact to be an internal library error (as the code does raise build_api_error(response) what confirms my assertions).

Maybe it possible that the problem is related to a internet connectivity? If your network (or the server fails), it can either fail to retrieve the JSON file or can retrieve an empty one. But, the library should inform you more clearly.

So, it will try to decode an empty file inside the JSON decoder, what causes the error.

A temporary workaround would be to brace your code with a try statement and to try again if it fails.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download