The Function block executes custom JavaScript, TypeScript, or Python code in your workflows. Transform data, perform calculations, or implement custom logic.
Outputs
<function.result>: The value returned from your function<function.stdout>: Console.log() output from your code
Example Use Cases
Data Processing Pipeline - Transform API response into structured data
API (Fetch) → Function (Process & Validate) → Function (Calculate Metrics) → ResponseBusiness Logic Implementation - Calculate loyalty scores and tiers
Agent (Get History) → Function (Calculate Score) → Function (Determine Tier) → Condition (Route)Data Validation and Sanitization - Validate and clean user input
Input → Function (Validate & Sanitize) → API (Save to Database)Example: Loyalty Score Calculator
// Process customer data and calculate loyalty score
const { purchaseHistory, accountAge, supportTickets } = <agent>;
// Calculate metrics
const totalSpent = purchaseHistory.reduce((sum, purchase) => sum + purchase.amount, 0);
const purchaseFrequency = purchaseHistory.length / (accountAge / 365);
const ticketRatio = supportTickets.resolved / supportTickets.total;
// Calculate loyalty score (0-100)
const spendScore = Math.min(totalSpent / 1000 * 30, 30);
const frequencyScore = Math.min(purchaseFrequency * 20, 40);
const supportScore = ticketRatio * 30;
const loyaltyScore = Math.round(spendScore + frequencyScore + supportScore);
return {
customer: <agent.name>,
loyaltyScore,
loyaltyTier: loyaltyScore >= 80 ? "Platinum" : loyaltyScore >= 60 ? "Gold" : "Silver",
metrics: { spendScore, frequencyScore, supportScore }
};import json
# Reference outputs from other blocks using angle bracket syntax
data = json.loads('<agent>')
purchase_history = data["purchaseHistory"]
account_age = data["accountAge"]
support_tickets = data["supportTickets"]
# Calculate metrics
total_spent = sum(p["amount"] for p in purchase_history)
purchase_frequency = len(purchase_history) / (account_age / 365)
ticket_ratio = support_tickets["resolved"] / support_tickets["total"]
# Calculate loyalty score (0-100)
spend_score = min(total_spent / 1000 * 30, 30)
frequency_score = min(purchase_frequency * 20, 40)
support_score = ticket_ratio * 30
loyalty_score = round(spend_score + frequency_score + support_score)
tier = "Platinum" if loyalty_score >= 80 else "Gold" if loyalty_score >= 60 else "Silver"
result = {
"customer": data["name"],
"loyaltyScore": loyalty_score,
"loyaltyTier": tier,
"metrics": {
"spendScore": spend_score,
"frequencyScore": frequency_score,
"supportScore": support_score
}
}
print(json.dumps(result))Python Support
The Function block supports Python as an alternative to JavaScript. Python code runs in a secure E2B cloud sandbox.
Enabling Python
Select Python from the language dropdown in the Function block. Python execution requires E2B to be enabled on your Sim instance.
If you don't see Python as an option in the language dropdown, E2B is not enabled. This only applies to self-hosted instances — E2B is enabled by default on sim.ai.
Python code always runs in the E2B sandbox, even for simple scripts without imports. This ensures a secure, isolated execution environment.
Returning Results
In Python, print your result as JSON to stdout. The Function block captures stdout and makes it available via <function.result>:
import json
data = {"status": "processed", "count": 42}
print(json.dumps(data))Available Libraries
The E2B sandbox includes the Python standard library (json, re, datetime, math, os, collections, etc.) and common packages like matplotlib for visualization. Charts generated with matplotlib are captured as images automatically.
The exact set of pre-installed packages depends on the E2B sandbox configuration. If a package you need isn't available, consider calling an external API from your code instead.
Matplotlib Charts
When your Python code generates matplotlib figures, they are automatically captured and returned as base64-encoded PNG images in the output:
import matplotlib.pyplot as plt
import json
data = json.loads('<api.data>')
plt.figure(figsize=(10, 6))
plt.bar(data["labels"], data["values"])
plt.title("Monthly Revenue")
plt.xlabel("Month")
plt.ylabel("Revenue ($)")
plt.savefig("chart.png")
plt.show()JavaScript vs. Python
| JavaScript | Python | |
|---|---|---|
| Execution | Local VM (fast) or E2B sandbox (with imports) | Always E2B sandbox |
| Returning results | return { ... } | print(json.dumps({ ... })) |
| HTTP requests | fetch() built-in | requests or httpx |
| Best for | Quick transforms, JSON manipulation | Data science, charting, complex math |
Best Practices
Large Inputs and Payload Limits
Function blocks receive their code, parameters, resolved references, and previous block context in an internal execution request. Sim can safely reference oversized workflow outputs, such as large loop.results or parallel.results, when you select a smaller nested field like <loop.results[0][0].id>. Larger values are stored in execution storage and passed around as small references until code explicitly reads them.
File outputs are metadata-first by default. Referencing <file.name>, <file.url>, or similar metadata does not hydrate file contents. In JavaScript functions without imports, a direct base64 reference like <readfile.file.base64> is automatically rewritten to a lazy server-side read so the base64 string does not cross the Function request body.
You can also call the helper explicitly:
const file = <readfile.file>;
const base64 = await sim.files.readBase64(file);sim.files.readBase64(file), sim.files.readText(file), sim.files.readBase64Chunk(file, { offset, length }), and sim.files.readTextChunk(file, { offset, length }) read from server-side execution storage under memory caps. sim.values.read(ref) explicitly reads a large execution value reference, and sim.values.readArray(ref) reads a manifest-backed large array. These helpers are available only in JavaScript functions without imports. JavaScript with imports, Python, and shell do not support these lazy helpers yet.
Very large full reads can still fail by design; use chunk helpers or return a file when you need to handle more data.
Use text chunks for text-like files such as logs, CSV, JSONL, and markdown:
const file = <readfile.file>;
const firstMegabyte = await sim.files.readTextChunk(file, {
offset: 0,
length: 1024 * 1024,
});
return firstMegabyte.split('\n').slice(0, 10);Use base64 chunks for binary files such as images, PDFs, audio, archives, or APIs that expect base64 input:
const file = <readfile.file>;
const firstMegabyteBase64 = await sim.files.readBase64Chunk(file, {
offset: 0,
length: 1024 * 1024,
});
return { name: file.name, chunk: firstMegabyteBase64 };Chunk offset and length are byte-based. For Unicode text, a chunk can split a multi-byte character at the boundary; use text chunks for approximate text processing and prefer smaller structured references when exact parsing matters.
Avoid passing a full large object into a Function block when you only need one field. For example, prefer <api.data.customerId> over <api.data> when the API response is large. If a JavaScript Function without imports references a whole large execution value, Sim automatically rewrites it to sim.values.read(...) at runtime under memory caps. If the value is a manifest-backed array, Sim rewrites it to sim.values.readArray(...) so array variables can stay compact between blocks.
For large generated data, write the result to a file or table with outputPath, outputSandboxPath, or outputTable instead of returning the entire payload inline.
- Keep functions focused: Write functions that do one thing well to improve maintainability and debugging
- Handle errors gracefully: Use try/catch blocks to handle potential errors and provide meaningful error messages
- Test edge cases: Ensure your code handles unusual inputs, null values, and boundary conditions correctly
- Optimize for performance: Be mindful of computational complexity and memory usage for large datasets
- Use console.log() for debugging: Leverage stdout output to debug and monitor function execution