Issue with Formatting LLM Output for Google Sheets through a Custom Function - Output variable seems always empty

Hi everyone,
I’m working on a workflow from Twitter, to LLM, formatting (for inserting formatted text, new line etc. into google sheet) (custom function) and inserting into google sheet.

LLM output:
"2025-07-20","### What the digital identity community was talking about last week\n\nDiscussions centered on SSI...\n\n• Bullet one\n• Bullet two"

My goal:

  • Take this LLM output as input in a Run Function block.
  • Convert all escaped \n into actual line breaks so Google Sheets shows them as formatted text in a single cell.
  • Return a single output variable (e.g., formattedRow) in valid CSV format:

The code I am using:

import csv
import io

def run(inputs):
    llm_output = inputs.get("llmOutput", "").strip()
    llm_output = llm_output.replace("\\n", "\n")

    output_stream = io.StringIO()
    writer = csv.writer(output_stream, quoting=csv.QUOTE_ALL)
    # Expecting the LLM output to already have the two columns separated by a comma
    try:
        reader = csv.reader(io.StringIO(llm_output))
        parsed = next(reader)
        writer.writerow([parsed[0], parsed[1]])
        final_csv = output_stream.getvalue().strip()
    except Exception as e:
        final_csv = f'"ERROR","Failed to parse: {e}"'

    return { "outputs": { "formattedRow": final_csv } }

The Configuration in that function:

config = {
  "configurationSections": [
    {
      "title": "Inputs",
      "items": [
        {
          "label": "LLM Output",
          "variable": "llmOutput",
          "type": "text"
        }
      ]
    },
    {
      "title": "Outputs",
      "items": [
        {
          "label": "Formatted Row Output",
          "variable": "formattedRow",
          "type": "text"
        }
      ]
    }
  ]
}

Problem:
The output variable is always empty, and the custom function does not seem to output anything - based on another block which I use afterward to “debug” (or the console).

Picture of my Custom Function variables:

Thanks a lot! I spent already some time debugging it with different AIs, but was unable to get it to run.
Pavol

Hi @PavolH,

Just to confirm, are you only adding the Custom Function to double-check the output from the LLM? If so, it might be simpler to reinforce the prompt and connect the Generate Text block directly to the Update Google Sheet block.

As for the Custom Function itself, the first issue I see in your screenshot is that you’re introducing a new variable with double curly braces. When defining a new variable, just type the word directly in the output field, and only use double curly braces when referencing an existing one. In your setup, that should be just formattedRow.

At the same time, it seems like your code doesn’t save the new values correctly. Here’s what you can do:

It also looks like your code may not be saving the new values correctly. Here are a couple of things you can try:

  1. Use the Generate button in the Custom Function block to get a starting point for the code and configurations:

  2. If you run into any issue, you can remix the Agent below to chat with AI and debug your Custom Function:
    Remix Custom Function Debugger Agent

Hope this helps!

Hello @Alex_MindStudio,

So, I am trying to format the output of the LLM in such a way that if it gets appended to the cell in my Google Sheet, it is inserted with New Lines (formatted). I have tried multiple prompts, but the output of the LLM was always inserted as an unformatted block of text.

This is why I was using the custom-function block. Maybe there is an easier way to do that?
I have already tried your debugger agent, but the output has not helped me unfortunately.

I have tried without the curly brackets, but unfortunately did not help - I might try to use Claude to debug it, or any other ai tools and as well the inbuild “generate code” function.

Regards
Pavol

Hi @PavolH,

In your code snippet above, you are defining a Python function called run() and then never invoking that function, so there is nothing for the code to do as it is currently written. Additionally, your function is not correctly interfacing with MindStudio to get and set variables, so even if you resolve that issue it’s not going to be working as expected.

The Google Sheets blocks have a built-in CSV parser and validator to handle text with line breaks or other special characters, so there should be no need to use a custom function here. Can you please share the debugger output from running your agent, specifically the Google Sheets actions? You can learn more about how to use the debugger here: Testing & Debugging Basics | MindStudio University