Azure / Azure/aistudio-copilot-sample
ai chat --interactive --function src/copilot_aisdk/chat:chat_completion raises JSONDecodeError for newline characters
- Dominant language
- Python
- Stars
- 321
- Forks
- 192
- PR merge metrics
- No merged PRs in 30d
Description
Hello,
Thank you for developing this aistudio copilot sample. I am writing to report what appears to be a handled but unexpected JSON decoding exception when using the interactive chat feature of this sample. This appears to be caused by the way newline characters are handled. The expected behavior is that the AI CLI properly converts strings to and from JSON format during the call and response cycle without raising an exception.
For example, when I run `ai chat --interactive --function src/copilot_aisdk/chat:chat_completion` from a powershell terminal within VS Code, it properly initializes the chat like so:

However, when I submit a follow up question, I receive the following error:

Notably, this error occurs in a temporary .py file called `tmpfrqkz1.tmp.py`, which is difficult to inspect as it appears to be removed after execution of the command. Opening the file quickly during a call reveals line 53 where the traceback refers to:

As well as the raised exception:

This error goes away if, for example, we replace the newline character (`\n`) with a newline character escaped with an extra backslash (`\\n`) by doing `chat_completion.choices[0].message.content.replace('\n', '\\n')`, like so:

However, then the result does not properly render the newline characters in the output:

Would it make sense to build in logic to make the string JSON compliant by escaping newline or other special characters before calling `json.loads(json_params)` so that they do not raise an exception? Or, as previously mentioned, would it make sense to use `json.loads(json_params, strict=False)` to silence the exception? Or is the json.decoder.JSONDecodeError exception with newline characters the intended behavior?
Thanks in advance for your help with this inquiry!
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.