Skip to content

BlueapiClient gives unhelpful error message when invalid JSON is returned #1472

Closed

Description

@oliwenmandiamond

When trying to use BlueapiClient, I got the following error message when trying to run the below:

from blueapi.client import BlueapiClient

bc = BlueapiClient.from_config_file("path/to/config.yaml")
devs = bc.devices
plans = bc.plans
python -i /home/xol73553/Documents/BLUESKY/i09_blueapi.py 
Traceback (most recent call last):
  File "/scratch/bluesky_development/blueapi/venv/lib64/python3.12/site-packages/requests/models.py", line 976, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/json/decoder.py", line 338, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/json/decoder.py", line 356, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xol73553/Documents/BLUESKY/i09_blueapi.py", line 22, in <module>
    devs = bc.devices
           ^^^^^^^^^^
  File "/usr/lib64/python3.12/functools.py", line 998, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/venv/lib64/python3.12/site-packages/observability_utils/tracing/decorators.py", line 151, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/client.py", line 307, in devices
    return DeviceCache(self._rest)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/client.py", line 111, in __init__
    for model in rest.get_devices().devices
                 ^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/rest.py", line 162, in get_devices
    return self._request_and_deserialize("/devices", DeviceResponse)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/venv/lib64/python3.12/site-packages/observability_utils/tracing/decorators.py", line 151, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/rest.py", line 290, in _request_and_deserialize
    deserialized = TypeAdapter(target_type).validate_python(response.json())
                                                            ^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/venv/lib64/python3.12/site-packages/requests/models.py", line 980, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The response for devices contains no json, so it provides a JSONDecodeError. The reason the response provides no valid json was because I was not logged in any more.

>>> bc.login()
Logging in
Please login from this URL:- https://identity.diamond.ac.uk/realms/dls/device?user_code=ULED-SLXT
Logged in and cached new token
>>> exit()

Now rerunning the script

python -i /home/xol73553/Documents/BLUESKY/i09_blueapi.py 
>>> 

It works! But the error message above didn't indicate the issue is due to not being logged in.

Acceptance critera

The client should instead make a check to see if the user is logged in first and then provide a useful error message to the user saying unable to retrieve plans / devices, please login first.

Activity

  1. tpoliaw commented on Apr 7, 2026

    @tpoliaw
    Contributor

    The reason the response provides no valid json was because I was not logged in any more

    I'm pretty sure it should return valid JSON when not logged in. I'm guessing this error is from oauth2-proxy redirecting the request and then returning HTML.

    The error could still be improved in the "server returned invalid json" case but I think this is more to do with deployment configuration than blueapi.

  2. changed the title [-]BlueapiClient gives unhelpful error message when not logged in[/-] [+]BlueapiClient gives unhelpful error message when invalid JSON is returned[/+] on Apr 7, 2026
  3. added
    c: clientRelates to client code
    deploymentStuff related to running in production container
    on Apr 7, 2026
  4. tpoliaw commented on Apr 8, 2026

    @tpoliaw
    Contributor

    Steps to reproduce in dev environment

    1. Start the system test services as normal docker compose -f tests/system_tests/compose.yaml up -d (see docs)
    2. Start blueapi with the system_test config uv run blueapi -c tests/system_tests/config.yaml serve
    3. Update (or create a new copy of) the config file to set the api to http://localhost:4180, leaving the server running
    4. Run an command via the CLI, eg uv run blueapi -c path/to/updated/config.yaml controller plans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: clientRelates to client codedeploymentStuff related to running in production containeronboardingGood for newcomers

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions