utils.llm.LLMChat._coerce_to_dict_or_list allows misformatted JSONs through

Open Beginner friendly
#159 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
python
Domain
ai

Research direction

Start in tinytroupe/utils/llm.py at _coerce_to_dict_or_list and reproduce the reported call with '[{[}]'. Inspect the enclosing-character check and compare it with genuinely empty dictionary or list responses. Done means malformed JSON is no longer silently returned as an empty result, while valid empty responses still work.

Written by the indexing model from the issue text.

Description

In certain cases (e.g. long JSON generated by gemini-3.5-flash or other models that tend to generate incomplete or misformed objects), function _coerce_to_dict_or_list can allow mismatched JSONs through as if they were empty:

>>> import tinytroupe.utils.llm
>>> tinytroupe.utils.LLMChat._coerce_to_dict_or_list(None, '[{[}]')
{}

This seems to be due to this check in the function:

https://github.com/microsoft/TinyTroupe/blob/f6ad1b3b228a1f96a945a49c2832ec06205c5dd3/tinytroupe/utils/llm.py#L934-L937

which only checks for the existence of dictionary enclosing characters, which can happen even within a misformed JSON.

This can result in silent errors such as TinyPersonFactory.sampling_dimensions being empty and therefore all generated agents would have none of the expected attributes.

Possible solutions:

  1. Add a simple sanity check through length or present characters (a truly empty dictionary response should not have anything other than a {}, [], and maybe a couple of whitespaces).
  2. Replace the character presence check with an equality check
Dominant language
Jupyter Notebook
Stars
7.6k
Forks
685
PR merge metrics
No merged PRs in 30d

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.

More from microsoft/TinyTroupe

All issues in microsoft/TinyTroupe

Similar issues

More AI Infra & Agents issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.