Code blocks let you run Python logic directly inside a Fin Procedure, giving you consistent, exact results for tasks where an LLM may be unpredictable.
When to use code blocks
Use code blocks for deterministic, repeatable logic — especially where Fin may otherwise be inconsistent:
Date & time calculations — business days, time zone conversions, deadline checks
Mathematical operations — refund amounts, percentages, currency conversions
String manipulation — formatting IDs, extracting substrings, regex matching
Data transformation — reshaping connector responses, filtering or mapping lists
Complex conditional logic — eligibility checks with multiple criteria
Tip: Use code blocks when the output must be exact and repeatable — not when it benefits from interpretation.
When not to use code blocks
Avoid code blocks for tasks Fin handles well naturally, such as:
Interpreting user intent
Generating conversational responses
How to use code blocks
1. Create a code block
Open the tool selector by typing @ or /
Select +Create code block
2. Write and test your code
The code block editor will open
Click Test code to run your logic, this opens the test panel
Input fields appear for any attributes you've referenced
Run tests and view results in JSON or list format
3. Understand attributes
Attributes are created from the returned response. For example, the following code:
return {
"result": {
"nested_result": "This is nested"
},
"date": "05-04-2025"
}Creates the following attributes:
A root attribute (the entire response)
Nested attributes: result, result.nested_result, and date
Note: The root attribute is always created and will always contain the full returned response, it doesn't need to be JSON.
4. Use the code block in a procedure
Return to the procedure editor
Open the tool selector by typing @ or /
Select Execute code — this lists all available code blocks
Choose your code block
Once selected, you can use the attributes throughout the procedure and continue as normal.
Available libraries
These Python standard library modules are whitelisted and pre-imported. You can use them directly without an import statement, though adding imports is recommended for clarity.
Module | Use for | Example |
math | Math functions | math.floor(3.7), math.ceil(2.1) |
decimal | Precise decimal arithmetic | Decimal("19.99") * Decimal("0.15") |
re | Regular expressions | re.match(r"\d{4}", order_id) |
datetime | Dates, times, durations | datetime.date.today(), timedelta(days=5) |
json | JSON encode/decode | json.loads(raw_string) |
random | Random number generation | random.randint(1, 100) |
zoneinfo | Timezone handling | ZoneInfo("America/New_York") |
time | Time-related functions | time.time() |
Note: Only top-level imports are allowed. Avoid submodule imports — for example, from json import loads is not supported.
Need more help? Get support from our Community Forum
Find answers and get help from Intercom Support and Community Experts







