Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ai chat --interactive --function src/copilot_aisdk/chat:chat_completion raises JSONDecodeError for newline characters #111

Open
yksnilowyrahcaz opened this issue Apr 17, 2024 · 0 comments

Comments

@yksnilowyrahcaz
Copy link

yksnilowyrahcaz commented Apr 17, 2024

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:

image

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

image

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:

image

As well as the raised exception:

image

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:

image

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

image

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant