Help with declarative agents implementing RAG

Johann Granados 0 Reputation points
2025-05-12T23:29:53.21+00:00

Hi

 

We are working on a declarative agent that uses an API plugin we created to extract data from SAP. We are using the CodeInterpreter capability to instruct the foundation model to generate code in Python to perform calculations and aggregations on the data returned by the API plugin. However, we are constantly getting errors from Copilot saying that the operation could not complete successfully, and usually it is because either the generated code in Python is not fully functional or the input data for that code is not properly formatted as a valid JSON payload (sometimes it is truncated, sometimes the quotes are missing for some properties or values, sometimes empty spaces are added to the property names, and other similar syntax issues).

 

I am attaching the instructions file we are using. We don’t know what else to do to solve the previous problem. Any help from you or your team would be greatly appreciated.

 

I’m looking forward to hearing from you.

 

Thank you very much!

Microsoft Copilot | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Ayush Giri 245 Reputation points Microsoft External Staff
    2025-05-13T06:14:39.46+00:00

    Hello Johann Granados,
    As you mentioned, can you please provide the instruction file, that will help me understand exactly how your agent is being guided and pinpoint where formatting, parsing, or logic might be breaking down.


  2. Ayush Giri 245 Reputation points Microsoft External Staff
    2025-05-22T17:26:51.7333333+00:00

    Hello Johann,
    Please try these:

    1. Validate and Clean JSON
      • Strip spaces from property names.
      • Ensure all keys/values are quoted.
      • Use json.loads() to confirm it’s valid before sending to the Code Interpreter.
    2. Don’t Use Code Interpreter for Logic
      • Do all calculations and aggregations outside the Code Interpreter.
      • Use Code Interpreter only to create files (Excel, PDF, charts, etc.).
    3. Use Safe JSON Access in Python
      • Use .get("key", default) to avoid missing key errors.
      • Skip records with missing or null values.
    4. Avoid Truncated Output
      • Ensure JSON output is in one complete block and not split or cut off.
      • Return large responses in chunks of 50 entries, each with valid JSON formatting.
    5. Auto-Retry for Python Errors
      • Retry up to 3 times if code fails, then return a clear error message.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.