# Odd error on call to Python custom function

**URL:** <https://community.mindstudio.ai/t/odd-error-on-call-to-python-custom-function/1640>\
**Category:** Bug Reports\
**Created:** [September 12, 2025, 6:38pm UTC](https://community.mindstudio.ai/t/odd-error-on-call-to-python-custom-function/1640 "2025-09-12T18:38:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken](https://yyz2.discourse-cdn.com/flex008/user_avatar/community.mindstudio.ai/ken/32/493_2.png) [@Ken](https://community.mindstudio.ai/u/Ken)\
**Post date:** [September 12, 2025, 6:38pm UTC](https://community.mindstudio.ai/t/odd-error-on-call-to-python-custom-function/1640/1 "2025-09-12T18:38:13Z")

</div>

(Can’t attach evidence here; it contains a proprietary API key. Please contact me via email.)

I’m building an agent for a client, and the following error occurs randomly on every run:

I created a simple Python script to count the characters from the text string returned by Scrape URL. Works about 98% of the time, but the other 2% throws this error:

```
**7 Executing "ScrapeSize"**
 **8 Error: SyntaxError: Unexpected token '<', "<html>**
**<h"... is not valid JSON

```

\*\*  
The function does nothing but count characters and return a numeric value. So I don’t understand where that’s coming from. The output from Scrape URL and input to the function is just plain text, which can be seen in the debugger log.

I can do an imprecise work-around with a Generate Text block, but it would be better (and cheaper) if the Python function was 100% reliable.

Any guidance you can offer would be gratefully appreciated!

Thank you!

---

<div class="post-metadata">

**Author:** ![Alex\_MindStudio](https://yyz2.discourse-cdn.com/flex008/user_avatar/community.mindstudio.ai/alex_mindstudio/32/27_2.png) [@Alex\_MindStudio](https://community.mindstudio.ai/u/Alex_MindStudio)\
**Post date:** [September 15, 2025, 4:02pm UTC](https://community.mindstudio.ai/t/odd-error-on-call-to-python-custom-function/1640/2 "2025-09-15T16:02:21Z")

</div>

Hi Ken,

Could you share the Custom Function code and Configurations?

---

<div class="post-metadata">

**Author:** ![Ken](https://yyz2.discourse-cdn.com/flex008/user_avatar/community.mindstudio.ai/ken/32/493_2.png) [@Ken](https://community.mindstudio.ai/u/Ken)\
**Post date:** [September 15, 2025, 4:20pm UTC](https://community.mindstudio.ai/t/odd-error-on-call-to-python-custom-function/1640/3 "2025-09-15T16:20:03Z")

</div>

**Code:**

def scrape\_size():

```
\# Get source variables data from config

text = ai\['vars'\]\['scrapedText'\]

max_size = ai\['vars'\]\['perItemTokenBudget'\]

print(f"Input Text Value: {text}")

print(f"Input Max Size: {max_size}")

chars = len(text)

words = len(text.split())

   

\# crude but solid approximations

tokens_est = max(chars // 4, int(words \* 1.3))

dest_var = ai\['config'\]\['tokens'\]

ai\['vars'\]\[dest_var\] = tokens_est

dest_var = ai\['config'\]\['tooLong'\]

ai\['vars'\]\[dest_var\] = (tokens_est > int(max_size or 3000))

```

# Execute the function

scrape\_size()

**Configuration:**

config = {

transitionType: ‘controlled’,

metadata: {

```
name: 'Scrape Size',

description: 'Determines whether a block of text is too long',

```

},

blockStyle: {

```
label: 'Scrape Size',

```

},

configurationSections: [

```
{

  title: 'Input Settings',

  items: \[

    {

      label: 'Scraped Text',

      type: 'inputVariable',

      variable: 'scrapedText',

    },

    {

      label: 'Max Size',

      type: 'inputVariable',

      variable: 'perItemTokenBudget',

    },

  \],

},

{

  title: 'Output Settings',

  items: \[

    {

      label: 'Token Count Estimate',

      type: 'outputVariableName',

      variable: 'tokens',

    },

    {

      label: 'Too Many Tokens?',

      type: 'outputVariableName',

      variable: 'tooLong',

    },

  \],

},

```

],

}

---

<div class="post-metadata">

**Author:** ![Alex\_MindStudio](https://yyz2.discourse-cdn.com/flex008/user_avatar/community.mindstudio.ai/alex_mindstudio/32/27_2.png) [@Alex\_MindStudio](https://community.mindstudio.ai/u/Alex_MindStudio)\
**Post date:** [September 16, 2025, 1:25pm UTC](https://community.mindstudio.ai/t/odd-error-on-call-to-python-custom-function/1640/4 "2025-09-16T13:25:19Z")

</div>

Hi @Ken,

It seems this could be happening when a Scrape URL block saves an HTML error or redirect page to a variable, so the function receives HTML instead of JSON.

Could you send me a direct message with the Debugger logs for that run? To do that, open the Debugger, select the run, click Share, and reply with the URL:

 ![image](https://canada1.discourse-cdn.com/flex008/uploads/mindstudio/original/1X/ae42f5bfab87b54bb00a38eae8c3744ef483d3bd.png)
