Function call for python code with invalid json

Two issues with passing functions to gpt-3.5-turbo-0613

If I use this function signature for python script

    def signature(self)->FunctionDescription:
        """Agent as tool
        """
        return {
            "name": "python",
            "description": "Useful to run scripts to analyze tables and spreadsheets using pandas.",
            "parameters": {
                "type": "object",
                "properties": {
                    "code": {
                        "type": "string",
                        "description": "Fully functional Python script. Must use print(...) to see the output values. ",
                    },
                },
                "required": ["code"],
            },
        }
  1. I get a code that has invalid “\n” in the code (json is not properly escaping the linebreak in the print)
# Print the results
print('Total Sales:')
print(total_sales)
print('
Average Sales:')
print(average_sales)
print('
Maximum Sales:')
print(max_sales)
print('
Minimum Sales:')
print(min_sales)
  1. However I noticed if I do not pass the function signature at all, it still gets called when needed!!! And that time with a string as “arguments” which is correct code but not what the API documents tell!

I have this hack that fixes the second issue

        function_name:str = function_call["name"]
        arguments:str = function_call["arguments"]
        if arguments:
            try:
                result["arguments"] = json.loads(arguments)
            except json.JSONDecodeError:
                result["arguments"] = {"arg": arguments}  # this is the hack

After this hack (and when function is not passed!!) I get this correct code as string instead of dictionary/object

# Print the insights
print("Weekly Sales Performance:")
print("-------------------------")
print("Total Sales:")
print(total_sales)
print("\nAverage Sales:")
print(average_sales)
print("\nMaximum Sales:")
print(max_sales)
print("\nMinimum Sales:")
print(min_sales)
2 Likes

Thanks for the info, hopefully it will help others with similar issues.

The problem is not passing the function signature (and still getting python function) is unexpected and could break in the future.
For now I am going to fix the returned string for code and remove \n if it is inside a quotation

def fix_code(code:str)->str:
    """Fix some common known issues with the returned code
    """
    code_list = list(code)
    quote_start = -1
    for q in ["'", '"']:
        for idx, ch in enumerate(code):
            if ch == q:
                if quote_start >= 0:
                    quote_start = -1
                else:
                    quote_start = idx
                continue
            if ch == "\n" and quote_start >= 0:
                code_list[idx] = r"\n"
    
    return "".join(code_list)

For sure, but it’s great to see the community sharing solutions and building things, thanks for your input.

I also experienced similar issues and decided to create a package to address this problem. You can find the package in PYPI: fuzy-jon