llamaIndex - ✅(Solved) Fix [Feature Request]: Output Schema Validation in FunctionTool [1 pull requests, 3 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
run-llama/llama_index#21094Fetched 2026-04-08 01:08:15
View on GitHub
Comments
3
Participants
2
Timeline
18
Reactions
0
Author
Timeline (top)
mentioned ×5subscribed ×5commented ×3labeled ×2

Error Message

Please add support for automatic output schema validation in FunctionTool. Ideally, FunctionTool should accept an optional output schema (e.g., a Pydantic model) and validate the function’s return value after execution, raising an error if the output does not conform.

PR fix notes

PR #21320: feat(#21094): implement output schema validation in FunctionTool

Description (problem / solution / changelog)

Description

This PR introduces output schema validation for FunctionTool by adding an optional output_schema to ToolMetadata and leveraging Pydantic validation across both the call and acall execution paths.

Note per AI guidelines: I used AI initially to help review the architectural approaches discussed in the issue comments. Together we verified the decision to place the schema on ToolMetadata, scaffolded the asynchronous unit test paths, and ran local validation.

Fixes #21094

New Package?

Did I fill in the tool.llamahub section in the pyproject.toml and provide a detailed README.md for my new integration or package?

  • Yes
  • No

Version Bump?

Did I bump the version in the pyproject.toml file of the package I am updating? (Except for the llama-index-core package)

  • Yes
  • No (Change is contained within llama-index-core)

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Your pull-request will likely not be merged unless it is covered by some form of impactful unit testing.

  • I added new unit tests to cover this change (Added async test paths to match your sync test paths)
  • I believe this change is already covered by existing unit tests

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added Google Colab support for the newly added notebooks.
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I ran uv run make format; uv run make lint to appease the lint gods

Changed files

  • llama-index-core/llama_index/core/tools/function_tool.py (modified, +6/-0)
  • llama-index-core/llama_index/core/tools/types.py (modified, +1/-0)
  • llama-index-core/tests/tools/test_base.py (modified, +78/-0)
RAW_BUFFERClick to expand / collapse

Feature Description

Currently, LlamaIndex’s FunctionTool only validates tool inputs using an input schema (e.g., via Pydantic), but does not validate tool outputs against an output schema.


Please add support for automatic output schema validation in FunctionTool. Ideally, FunctionTool should accept an optional output schema (e.g., a Pydantic model) and validate the function’s return value after execution, raising an error if the output does not conform.

Value of Feature

  • Ensures structured results are always validated, improving reliability and safety.
  • Aligns output handling with input validation, making the developer experience more consistent.
  • Reduces the need for manual output validation code in user projects.
  • Enables full use of MCP tool definitions, which can provide both input and output schemas.

extent analysis

Fix Plan

To add support for automatic output schema validation in FunctionTool, we will:

  • Modify the FunctionTool to accept an optional output schema
  • Validate the function's return value against the output schema after execution

Code Changes

from pydantic import BaseModel, ValidationError

class OutputSchema(BaseModel):
    # define the output schema here
    result: str

def validate_output(output, output_schema):
    try:
        output_schema(**output)
    except ValidationError as e:
        raise ValueError(f"Output does not conform to schema: {e}")

class FunctionTool:
    def __init__(self, func, input_schema, output_schema=None):
        self.func = func
        self.input_schema = input_schema
        self.output_schema = output_schema

    def execute(self, input_data):
        # validate input data
        self.input_schema(**input_data)
        
        # execute the function
        output = self.func(**input_data)
        
        # validate output data if output schema is provided
        if self.output_schema:
            validate_output(output, self.output_schema)
        
        return output

# example usage
def example_func(input_data):
    return {"result": "example result"}

input_schema = BaseModel
output_schema = OutputSchema

function_tool = FunctionTool(example_func, input_schema, output_schema)
input_data = {}
output = function_tool.execute(input_data)

Verification

To verify that the fix worked, test the FunctionTool with different output schemas and input data. Check that it raises an error when the output does not conform to the schema.

Extra Tips

  • Use Pydantic's built-in validation features to define the output schema.
  • Consider adding support for custom validation logic if needed.
  • Document the new feature and provide examples of usage.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING