stan-dev / stan-dev/pystan

`KeyError: 'message'` occurs when trying to build a model with wrong datatype

Open
#379 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
366
Forks
61
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

When the data has wrong datatype (e.g. values of dataset are str type), pystan raises KeyError: 'message'.

The full Traceback is:

Traceback (most recent call last):
  File "/workdir/main.py", line 25, in <module>
    posterior = stan.build(model, data=data)
  File "/usr/local/lib/python3.9/site-packages/stan/model.py", line 519, in build
    return asyncio.run(go())
  File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.9/site-packages/stan/model.py", line 511, in go
    raise RuntimeError(resp.json()["message"])
KeyError: 'message'

This is possibly a problem of the httpstan.
But I think this pystan's behavior isn't intended. So I'm reporting here.

Describe your system
  • CPU type: x86-64
  • Windows 10 19044.2251
  • WSL 2 Ubuntu 20.04
  • Docker Desktop 4.10.1
  • Python 3.9 (I also checked occurrence of the same error in 3.8 and 3.11)
  • pystan 3.6.0 and httpstan 4.9.1 (I also checked in pystan==3.2.0 and httpstan==4.5.0)
Steps/Code to Reproduce

My code sample consists of two files. To run them, please place them at the same directory.

├── Dockerfile
└── main.py
# Dockerfile
FROM python:3.9
COPY main.py .
RUN pip3 install httpstan==4.9.1 pystan==3.6.0
CMD ["python3", "main.py"]
# main.py
import stan


model = """
data {
  int<lower=0> N;
  vector[N] x;
  vector[N] y;
}
parameters {
  real alpha;
  real beta;
  real<lower=0> sigma;
}
model {
  y ~ normal(alpha + beta * x, sigma);
}
"""

# data has str values
data = {
    'y': ['58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72'],
    'x': ['115', '117', '120', '123', '126', '129', '132', '135', '139', '142', '146', '150', '154', '159', '164'],
    'N': 15
}
posterior = stan.build(model, data=data)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with Dockerfile and main.py using the supplied string-valued data, then inspect the stan/model.py build path shown in the traceback and the response it parses. Done means the invalid datatype produces a useful reported error instead of KeyError: 'message'.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.