Best Practices for AI-Generated Python/C# coding

Hi everyone,

I often rely on AI-generated Python code to tidy up and develop scripts for ghpython nodes in Grasshopper for Rhino 3D (v8). While this works well for simpler tasks, I’m not an expert in Python, so when things get more complex—especially with data structures like trees—I’ve noticed that the prompts I use don’t always generate the desired results.

Here’s a common example of how I structure my prompts:

You are an expert Python3 programmer. Write a Python 3 script (uses CPython) for a ghpython node in Grasshopper for Rhino 3D 8, with the RhinoCommon 8.0 API and the RhinoScriptSyntax API and Python library, .NET functionality. 

- Add a descriptive message for this Grasshopper component by using ghenv.Component.NickName with the prefix 'Py3_'. 
- Name Grasshopper parameters and variables by PEP 8 convention (lower_case_with_underscores)."

Create a script or modify the code pasted below if provided with the following requirements:

However, when handling more complex inputs (like data trees), the AI-generated code sometimes doesn’t handle the structures properly, leading to errors. Additionally, it’s difficult to copy/paste complex data structures into the AI prompt, as they often get flattened into text, which distorts the structure making it hard to debug. I’m sure many of you have faced similar issues!

Yes, I know learning to code is step one, but AI can be a helpful assistant along the way to streamline the process or provide additional support.

So, I’m asking:
What kind of prompts or strategies do you use to generate accurate AI-assisted Python or C# code for Grasshopper, especially for complex data structures?

The goal of this thread is to gather insights on what works best for improving the accuracy of AI-generated code.

If you have any useful prompts, tips, or experiences to share, I’d really appreciate it!

Here is my experience

ChatGPT or other LLM are basically word estimators. It predicts 1 word after the other, and depending on how precise the given information is, the more likely it predicts the next word during an answer. The more precise you refine the information within a session, the better it predicts the next answers, because it includes the previous conversation. However it will reach a point quickly where more information does not improve the answer. It could even worsen the answers at some point. A good strategy therefore is to delete the session and start a new one with providing the useful results from the last session.

Know the basics! It is obvious, the more general your problem is, the better the results are. If you work with a niche API, like Grasshopper, then there is little correct information out there. You can however feed ChatGPT with the particular API documentation.
Try to break down a problem to a general solution. Ideally a custom component/script is only interfacing Grasshopper for the in- and output boundary, and the Rhinocommon API for solving geometrical problems. But in order to do so, you need to get a basic understanding of programming. I think this is what most amateurs or junior developers get wrong. You don’t need to learn a specific technology or library. Its more about how to work with different programming interfaces. Once you know this, you can replace Grasshopper with any other application and just get help from ChatGPT to understand the properties of the particular Interface.

2 Likes