Calling LLMs from custom functions in flows
In single-tenant environments with advanced view enabled, you can call an LLM from custom function code to get text or structured outputβfor example, to drive extraction, classification, or validation with AI. Implement Python in the projectβs legacy scripts folder or module, register entrypoints, and reference functions by name in the step configuration. Model and provider come from the tenantβs configured LLM provider and the AI runtime model.
Calling LLMs from custom functions works differently in the app editor, see Calling LLMs from custom functions.
Availability
In the flow editor, llm_client is supported in these steps and event hooks:
Design considerations
When you use the LLM client in flow steps, consider the following:
-
The LLM client bypasses the platformβs built-in guardrails. Use it only where the dedicated agent steps canβt meet the requirement β for example, where custom preprocessing logic must run before an LLM call, or where an external data source must be queried and incorporated into a prompt. In all other cases, use the Agent classifier and Agent extract steps.
-
Contain LLM client calls to a single, well-defined step. Donβt scatter custom LLM calls across multiple UDFs throughout the flow.
-
When you call an LLM directly through the client rather than through a dedicated agent step, the following donβt apply automatically:
-
Grounding β The model isnβt constrained to return values from the source document. Enforce grounding through careful prompting. For example, if using the LLM client for classification, the prompt must explicitly instruct the model to return only one of the defined class names and handle cases where the document doesnβt match any category.
-
Schema validation β The platform doesnβt validate that LLM output matches an expected structure. Output parsing and error handling are the developerβs responsibility. Error handling must account for both external API failures and LLM output validation failures.
-
Rate limit management β Multiple LLM client calls across a flow at high document volume can quickly exhaust Azure OpenAI or Vertex AI rate limits, causing flows to fail.
-
Using the LLM client in flow steps
Calling an LLM from a flow step involves the following components and processes.
-
Legacy scripts β Add your Python function to the projectβs legacy scripts folder or module, then reference the function by name in the step configuration.
-
Registration β Decorate the entrypoint with
@register_fn(provenance=False)frominstabase.provenance.registrationso the platform can register and invoke it. -
Clients β Declare
CLIENTSas a positional argument in your function signature. The flow runtime passes it in automatically. Use attribute access to reference clients β for example,CLIENTS.llm_clientandCLIENTS.ibfileβ rather than dict-style access with.get('llm_client'). -
LLM usage β Call
generate_contentonllm_clientusing the same parameters described in the Call generate_content subsection below.
When you wire the function in the flowβfor example for a pre-flow or post-flow eventβinvoke it by passing CLIENTS according to your stepβs formula, such as llm_step(CLIENTS). Use the function name you registered and the variable order your step expects (see Custom functions in flow).
Get llm_client from CLIENTS
When the LLM client is available, the CLIENTS object exposes an LLM client (llm_client) and a file client (ibfile). Use the file clientβs read_file to read document content. Use the LLM clientβs generate_content method to send a prompt to the model and get a response (optionally with file data or a response schema for structured output). If the LLM client isnβt available for a given run (for example, the step doesnβt support it), guard the call before use, for example if CLIENTS.llm_client is None.
Example: Map UDF with structured output
The following example uses @register_fn, takes CLIENTS as a positional argument together with Map UDF variables, reads a file with CLIENTS.ibfile, and writes model output into out_files. Set the Map UDF step formula to match your declared parametersβfor example map_with_llm(INPUT_RECORD, STEP_FOLDER, CLIENTS).
Adjust filenames, MIME type, schema, formula, and return shape to match your step.
