New PyStan installation and ValueError: The JSON document has an improper structure

Hi there,

I’m just trying to get started with stan programming, but I keep running into pystan installation/stan.build issues. I’ve gone through the previous threads that seem to suggest a similar error, but nothing I’ve tried has worked so far.

My installation process was to activate my desired project-specific conda environment (dissertation) and then install pystan using:

conda install -c conda-forge pystan

The code below was taken from the PyStan documentation page here (PyStan — pystan 3.7.0 documentation) and is what I’ve been using to test the installation.

import stan
import nest_asyncio
nest_asyncio.apply()

schools_code = """
data {
  int<lower=0> J;         // number of schools
  real y[J];              // estimated treatment effects
  real<lower=0> sigma[J]; // standard error of effect estimates
}
parameters {
  real mu;                // population treatment effect
  real<lower=0> tau;      // standard deviation in treatment effects
  vector[J] eta;          // unscaled deviation from mu by school
}
transformed parameters {
  vector[J] theta = mu + tau * eta;        // school treatment effects
}
model {
  target += normal_lpdf(eta | 0, 1);       // prior log-density
  target += normal_lpdf(y | theta, sigma); // log-likelihood
}
"""

schools_data = {"J": 8,
                "y": [28,  8, -3,  7, -1,  1, 18, 12],
                "sigma": [15, 10, 16, 11,  9, 11, 10, 18]}

posterior = stan.build(schools_code, data=schools_data)

Running the above code continues to yield the following error:

Error handling request
Traceback (most recent call last):
  File "/Users/computer/miniconda3/envs/dissertation/lib/python3.10/site-packages/aiohttp/web_protocol.py", line 433, in _handle_request
    resp = await request_handler(request)
  File "/Users/computer/miniconda3/envs/dissertation/lib/python3.10/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
  File "/Users/computer/miniconda3/envs/dissertation/lib/python3.10/site-packages/httpstan/views.py", line 103, in handle_create_model
    _, stanc_warnings = httpstan.compile.compile(program_code, stan_model_name)
  File "/Users/computer/miniconda3/envs/dissertation/lib/python3.10/site-packages/httpstan/compile.py", line 37, in compile
    completed_process = subprocess.run(run_args, capture_output=True, timeout=1)
  File "/Users/computer/miniconda3/envs/dissertation/lib/python3.10/subprocess.py", line 503, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/Users/computer/miniconda3/envs/dissertation/lib/python3.10/subprocess.py", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/Users/computer/miniconda3/envs/dissertation/lib/python3.10/subprocess.py", line 1847, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: PosixPath('/Users/computer/miniconda3/envs/dissertation/lib/python3.10/site-packages/httpstan/stanc')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[4], line 1
----> 1 model = stan.build(model_code, data = data)

File ~/miniconda3/envs/dissertation/lib/python3.10/site-packages/stan/model.py:519, in build(program_code, data, random_seed)
    516         return Model(model_name, program_code, data, param_names, constrained_param_names, dims, random_seed)
    518 try:
--> 519     return asyncio.run(go())
    520 except KeyboardInterrupt:
    521     return

File ~/miniconda3/envs/dissertation/lib/python3.10/site-packages/nest_asyncio.py:35, in _patch_asyncio.<locals>.run(main, debug)
     33 task = asyncio.ensure_future(main)
     34 try:
---> 35     return loop.run_until_complete(task)
     36 finally:
     37     if not task.done():

File ~/miniconda3/envs/dissertation/lib/python3.10/site-packages/nest_asyncio.py:90, in _patch_loop.<locals>.run_until_complete(self, future)
     87 if not f.done():
     88     raise RuntimeError(
     89         'Event loop stopped before Future completed.')
---> 90 return f.result()

File ~/miniconda3/envs/dissertation/lib/python3.10/asyncio/futures.py:201, in Future.result(self)
    199 self.__log_traceback = False
    200 if self._exception is not None:
--> 201     raise self._exception.with_traceback(self._exception_tb)
    202 return self._result

File ~/miniconda3/envs/dissertation/lib/python3.10/asyncio/tasks.py:232, in Task.__step(***failed resolving arguments***)
    228 try:
    229     if exc is None:
    230         # We use the `send` method directly, because coroutines
    231         # don't have `__iter__` and `__next__` methods.
--> 232         result = coro.send(None)
    233     else:
    234         result = coro.throw(exc)

File ~/miniconda3/envs/dissertation/lib/python3.10/site-packages/stan/model.py:486, in build.<locals>.go()
    483 resp = task.result()
    485 if resp.status != 201:
--> 486     match = re.search(r"""ValueError\(['"](.*)['"]\)""", resp.json()["message"])
    487     if not match:  # unknown error, should not happen
    488         raise RuntimeError(resp.json()["message"])

File ~/miniconda3/envs/dissertation/lib/python3.10/site-packages/stan/common.py:25, in HTTPResponse.json(self)
     23 def json(self) -> dict:
     24     # mypy 0.961 complains that simdjson lacks a `loads`.
---> 25     return simdjson.loads(self.content)

File ~/miniconda3/envs/dissertation/lib/python3.10/site-packages/simdjson/__init__.py:52, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kwargs)
     43 """
     44 Same as the built-in json.loads(), with the following exceptions:
     45 
   (...)
     49     - cls is ignored.
     50 """
     51 parser = Parser()
---> 52 return parser.parse(s, True)

File simdjson/csimdjson.pyx:452, in csimdjson.Parser.parse()

ValueError: The JSON document has an improper structure: missing or superfluous commas, braces, missing keys, etc.

As I said, I’ve found similar issues about the installation and this error being raised, but I haven’t been able to get past my own. Any advice would be appreciated. Let me know if there is any more information that would be helpful.

Thank you for your time.

  • Operating System: Mac OS Monterey 12.6, intel i7 processor
  • Python Version: python 3.10.8
  • PyStan Version: 3.6.0 (httpstan: 4.6.1)
  • Compiler/Toolkit: clang version 14.0.6

…or should I just use CMDStanPy?

Did you try to use pypi for pystan?