Skip to content

feat: Add tool contract validation mechanism#343

Open
SHINING2024 wants to merge 1 commit into
calesthio:mainfrom
SHINING2024:feat/tool-contract-validation
Open

feat: Add tool contract validation mechanism#343
SHINING2024 wants to merge 1 commit into
calesthio:mainfrom
SHINING2024:feat/tool-contract-validation

Conversation

@SHINING2024

Copy link
Copy Markdown

Overview

Add a comprehensive tool contract validation system to ensure inputs and outputs conform to defined schemas, improving reliability and catching issues early.

Motivation

  • No validation: Currently, tools accept inputs without validation, leading to cryptic errors
  • Debug difficulty: Hard to debug issues from invalid inputs/outputs
  • Contract enforcement: Schemas are defined but not enforced
  • Better error messages: Clear validation errors help users fix issues faster

Technical Implementation

New features in tools/base_tool.py:

  • validate_inputs(inputs) - Validates input against input_schema
  • validate_outputs(outputs) - Validates output against output_schema
  • execute_safe(inputs) - Wraps execute with validation + error handling
  • ValidationError - New exception class for validation failures
  • Optional jsonschema dependency (graceful fallback if not installed)

Features

✅ Input validation using JSON Schema
✅ Output validation using JSON Schema
✅ Clear, descriptive error messages
✅ Graceful fallback if jsonschema not installed
✅ No breaking changes
✅ Backward compatible

Usage Example

# In your tool subclass, define schemas:
class MyTool(BaseTool):
    input_schema = {
        "type": "object",
        "required": ["input_text"],
        "properties": {
            "input_text": {"type": "string"}
        }
    }
    output_schema = {
        "type": "object",
        "required": ["result"]
    }

# Then use validation:
is_valid, error = tool.validate_inputs({"input_text": "hello"})
result = tool.execute_safe({"input_text": "hello"})

Testing

  • ✅ Verify validation works with valid inputs
  • ✅ Verify validation catches invalid inputs
  • ✅ Test graceful fallback without jsonschema
  • ✅ Test execute_safe error handling

- Create lib/logger.py with centralized logging interface
- Support multiple log levels (DEBUG, INFO, WARNING, ERROR, CRITICAL)
- Add console and optional file logging
- Provide convenience functions (debug(), info(), warning(), error())
- Add structured log formatting with timestamps
- No breaking changes to existing code
@SHINING2024 SHINING2024 requested a review from calesthio as a code owner July 10, 2026 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant