Skip to Content

Prompt Optimization with Tool Calling and Response Formatting

This tutorial demonstrates how to use Prompt Optimization in SAP AI Core for tool calling scenarios using a BFCL v3 dataset, and pairs it with SAP AI Core's **Response Formatting** feature so you can enforce structured output in two complementary ways. The process loads and normalizes a BFCL v3 parallel-multiple dataset, splits it into train and test sets, uploads all files to AI Core's built-in dataset storage, registers a dataset artifact, pushes a base prompt template to the Prompt Registry, and runs an optimization execution targeting Gemini 2.5 Pro with GPT-4o as the reference model using the `JSON_Match` metric. Alongside the optimizer, it introduces the Orchestration Service `response_format` parameter (`text`, `json_object`, `json_schema`) — an API-level way to guarantee valid JSON independent of prompt wording — and builds a JSON Schema from the unioned tool definitions. After completion, the optimized prompt is retrieved and compared against the base prompt through live inference, both with and without a `response_format` schema attached.
You will learn
  • How to load and normalize BFCL v3 parallel-multiple data into the SAP optimizer golden format.
  • How Response Formatting works (text, json_object, json_schema) and how it complements prompt optimization.
  • How to build a json_schema response_format from the unioned BFCL tool definitions.
  • How to upload train, test, tools, and prompt template files to AI Core dataset storage.
  • How to register a dataset artifact linking the uploaded folder to the genai-optimizations scenario.
  • How to create and register a base prompt template in the Prompt Registry.
  • How to configure and run prompt optimization via Python SDK and Bruno.
  • How to monitor execution progress and retrieve the optimized prompt.
  • How to compare base vs optimized prompt outputs through live inference, with and without a response_format schema.
I321506Smita NaikJuly 29, 2026
Created by
I321506
July 29, 2026
Contributors
I321506

Prerequisites

  1. BTP Account
    Set up your SAP Business Technology Platform (BTP) account.
    Create a BTP Account
  2. For SAP Developers or Employees
    Internal SAP stakeholders should refer to the following documentation: How to create BTP Account For Internal SAP Employee, SAP AI Core Internal Documentation
  3. For External Developers, Customers, or Partners
    Follow this tutorial to set up your environment and entitlements: External Developer Setup Tutorial, SAP AI Core External Documentation
  4. Create BTP Instance and Service Key for SAP AI Core
    Follow the steps to create an instance and generate a service key for SAP AI Core:
    Create Service Key and Instance
  5. AI Core Setup Guide
    Step-by-step guide to set up and get started with SAP AI Core:
    AI Core Setup Tutorial
  6. An Extended SAP AI Core service plan is required, as the Generative AI Hub is not available in the Free or Standard tiers. For more details, refer to
    SAP AI Core Service Plans
  7. You have access to the genai-optimizations scenario and have the required roles such as genai_manager or custom_evaluation.
  8. A BFCL v3 dataset file (e.g., BFCL_v3_parallel_multiple_10tools.json) is available locally.
  9. A running Orchestration Service deployment in your resource group. Response Formatting and the live inference comparison are executed through the Orchestration /completion endpoint, so you need its deployment URL. See Create a Deployment for Orchestration.
  • Step 1

    Before starting this tutorial, ensure that you:
    - Understand the basics of Generative AI workflows in SAP AI Core.
    - Are familiar with function calling / tool calling concepts in LLMs.
    - Are familiar with creating and managing prompt templates and artifacts in SAP AI Core.
    - Understand, at a high level, that there are two complementary ways to push a model toward structured output:
    - Prompt optimization — automate the trial-and-error of prompt engineering so the model’s reasoning reliably produces the right structure.
    - Response Formatting — an Orchestration Service parameter that constrains the model’s output at the API level, independent of prompt wording.
    - Have completed the Quick Start tutorial or equivalent setup for SAP AI Core access.

  • Step 2
    • Prompt Optimization for tool calling connects the Prompt Registry, AI Core Dataset Storage, and ML Tracking Service to form an end-to-end optimization workflow. Response Formatting sits on the Orchestration Service and is applied at inference time.
    • A BFCL v3 dataset is loaded, normalized, and split into train and test sets. All four files (train goldens, test goldens, tool definitions, and prompt template) are uploaded to a shared folder in AI Core’s built-in dataset storage.
    • The shared folder is registered as a single artifact under the genai-optimizations scenario.
    • The base prompt template is pushed to the Prompt Registry.
    • An optimization configuration links the artifact, base prompt, reference model (gpt-4o:2024-08-06), target model (gemini-2.5-pro:001), and the JSON_Match metric.
    • During execution, the optimizer iteratively refines the prompt. Metrics are tracked in the ML Tracking Service, and the optimized prompt is saved back to the Prompt Registry.
    • Separately, a JSON Schema is built from the unioned tool definitions. At inference time it can be attached as a response_format to the Orchestration call to guarantee the wire format is valid JSON — regardless of prompt wording.
    • After completion, the base and optimized prompts are fetched and compared via live inference, both with and without the response_format schema.
  • Step 3

    For hands-on execution and end-to-end reference, use the accompanying notebook Prompt_Optimization_With_Tool_Calling_And_Response_Formatting.ipynb. It runs the entire pipeline top to bottom — from loading the BFCL dataset and uploading files, through configuration creation, execution, monitoring, and the inference comparison — and includes a Response Formatting primer right after the connection step.

    💡 Run the cells in order, top to bottom — later cells depend on variables created earlier (client, configuration_id, execution_id, tool_call_schema, etc.). Configure your .env file and BFCL dataset path before executing.

    To use the notebook:
    - Download and open Prompt_Optimization_With_Tool_Calling_And_Response_Formatting.ipynb in your preferred environment (e.g., VS Code, JupyterLab).
    - Place your BFCL v3 dataset file (e.g., BFCL_v3_parallel_multiple_10tools.json) in the same directory.
    - Configure your .env file with your AI Core credentials.
    - Execute the cells in order to reproduce the complete prompt optimization with tool calling and response formatting workflow.


  • Step 4

  • Step 5

    Before running the pipeline, define the key configuration constants used throughout the notebook.

    • The reference model (REFERENCE_MODEL, e.g. gpt-4o:2024-08-06) acts as a teacher the optimizer compares against while searching for a better prompt.
    • The target model(s) (TARGET_MODELS) are the model(s) the final optimized prompt is actually tuned for.
    • Train samples (N_TRAIN_SAMPLES = 25) are used to generate and refine candidate prompts; test samples (N_TEST_SAMPLES = 15) are held out and only used to score each candidate, so the reported score reflects genuine generalization.
    • JSON_Match does a structural comparison between the candidate’s JSON output and the golden answer — simpler to set up than a custom LLM-as-a-judge metric and well suited to exact structural correctness.

  • Step 6

    The BFCL v3 dataset contains parallel and multi-tool function calling samples. The notebook uses a robust reader that handles three file formats — JSON array, standard JSONL, and concatenated JSON objects — before normalizing samples into the SAP optimizer golden format. The optimizer expects each example as a golden record: {"fields": {"question": ...}, "answer": "<JSON string>"}.


  • Step 7

    All four files — train goldens, test goldens, tool definitions, and the prompt template — are serialized locally and then uploaded to a shared folder in AI Core’s dataset storage. The shared folder is then registered as a single artifact.


  • Step 8

    The base prompt template is pushed to the Prompt Registry. It is intentionally minimal — system "You are a helpful assistant." and user {{?question}} — so the optimizer has maximum room to add structure (JSON-only output instructions, tool schemas, reasoning steps) and you can clearly see the value it adds in the final comparison.


  • Step 9

    The optimization configuration links the artifact, base prompt, reference model, target model, and metric into one executable setup. Creating a configuration does not run anything yet — think of it as saving a recipe.

    ⚠️ Note: Model availability and versions (for example, gpt-4o:2024-08-06, gemini-2.5-pro:001) may vary across SAP AI Core tenants. Always verify available models in Generative AI Hub → Models before use.
    For the latest updates, refer to SAP Note 3437766 – Model Availability and Support for Generative AI Hub.


  • Step 10

    After registering the configuration, trigger the optimization execution. The optimizer iteratively refines the base prompt using the train goldens and evaluates candidate prompts against the test goldens using the JSON_Match metric.


  • Step 11

    After triggering the execution, monitor its status. The execution transitions through UNKNOWNRUNNING (with progress updates) → COMPLETED. Expect roughly 20–30 minutes for 25 train + 15 test samples. The notebook also fetches logs automatically if the execution fails.


  • Step 12

    Once the execution completes, the optimized prompt is stored in the Prompt Registry. Retrieve it by its ID to inspect how the optimizer refined the base prompt.


  • Step 13

    After retrieving both prompt templates, run live inference on the same test questions to compare output quality between the base and optimized prompts — and, additionally, demonstrate what response_format=json_schema adds on top of the optimized prompt.


  • Step 14

    In this tutorial, you optimized a function-calling prompt using SAP AI Core with a BFCL v3 dataset, and paired it with Response Formatting for API-level structured-output guarantees:

    1. Learned Response Formatting — the three response_format modes (text, json_object, json_schema) and how they complement prompt optimization by constraining output at the API level rather than through prompt wording.
    2. Loaded and normalized the BFCL v3 dataset — using a robust multi-format reader and normalizing tool definitions to OpenAI ChatCompletions format.
    3. Split the dataset into 25 train goldens and 15 test goldens, built a union of all tool definitions, and derived a json_schema response_format from those tools.
    4. Uploaded four files (train, test, tools, prompt template) to a shared folder in AI Core’s built-in dataset storage via the /lm/dataset/files endpoint.
    5. Registered a dataset artifact linking the shared folder to the genai-optimizations scenario.
    6. Pushed the base prompt template (bfcl-tool-base:0.0.1) to the Prompt Registry.
    7. Created an optimization configuration with 15 parameter bindings including the JSON_Match metric, reference model (gpt-4o:2024-08-06), and target model (gemini-2.5-pro:001).
    8. Triggered and monitored the execution — tracking real-time progress from UNKNOWN through RUNNING to COMPLETED.
    9. Retrieved the optimized prompt (bfcl-tool-optimized-gemini25:0.0.1) from the Prompt Registry.
    10. Compared base vs optimized prompts via live inference — confirming the optimization WIN — and demonstrated the optimized prompt with and without a response_format schema, showing how prompt optimization and Response Formatting combine for defense-in-depth structured output.
Back to top