Skip to content

Conversation

@Zoe14
Copy link
Contributor

@Zoe14 Zoe14 commented Nov 20, 2025

When CodeAgent currently won't know the tool signature if it involves an array. it would just be dict.

claude and others added 5 commits November 5, 2025 22:28
…ype hints

The to_code_prompt() method was only showing the top-level type (e.g., "array")
without showing what the array contains. This made the generated function
signatures less informative for tools with array parameters.

Changes:
- Added _schema_to_python_type() helper method that converts JSON schema types
  to Python type hint strings (e.g., {"type": "array", "items": {"type": "string"}}
  becomes "list[str]")
- Updated to_code_prompt() to use the helper for both input and output types
- The method now properly handles:
  - Array types with items: array -> list[item_type]
  - Dict types with additionalProperties: object -> dict[str, value_type]
  - Union types: ["string", "integer"] -> str | int
  - Basic types: string -> str, integer -> int, number -> float, etc.
  - Nested types: list[list[int]], dict[str, list[str]], etc.

- Updated test expectations to match the improved Python type hint output

Example output change:
Before: def get_weather(locations: array) -> dict:
After:  def get_weather(locations: list[str]) -> dict:

Fixes the issue where array parameters didn't show their item types in
code agent prompts, making it clearer for LLMs what types to use.
This test verifies that the to_code_prompt() method properly extracts array
item types and converts them to Python type hints instead of showing generic
"array" types.

The test covers:
1. Array of strings: list[str]
2. Array of integers with dict return types: list[int], dict[str, str]
3. Simple arrays without items: list
4. Nested arrays: list[list[int]]

Each test case verifies that:
- The output contains proper Python type hints (list[type])
- The output does NOT contain generic JSON schema types (array)
- Complex types like dict[str, str] are properly formatted

This directly tests the fix for the issue where to_code_prompt() was only
showing top-level types without recursing into array items.
Adds test case for arrays containing objects with properties, which represents
a more complex nested structure commonly used in APIs and UI frameworks.

Example: An array of option objects where each option has 'value', 'label',
and optionally 'description' properties.

Schema structure:
{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "value": {"type": "string"},
      "label": {"type": "string"},
      "description": {"type": "string", "nullable": true}
    }
  }
}

Expected output: options: list[dict]

This test verifies that the type extraction properly handles:
- Array of objects (not just primitives)
- Objects with complex property definitions
- Nullable properties within nested structures

The implementation correctly converts this to list[dict] since objects with
specific properties are represented as dict in Python type hints.
The original implementation was hardcoded with inline type mappings that were
fragile and difficult to maintain. This refactoring improves the code by:

Changes:
1. Added JSON_SCHEMA_TO_PYTHON_TYPE mapping at module level
   - Centralizes the JSON schema to Python type conversion
   - Makes it easy to add/modify type mappings in one place
   - Provides clear documentation of supported type mappings
   - Reverse mapping of CONVERSION_DICT and _BASE_TYPE_MAPPING

2. Simplified _schema_to_python_type method
   - Removed hardcoded type_mapping dictionary from method
   - Uses centralized JSON_SCHEMA_TO_PYTHON_TYPE mapping
   - Cleaner, more readable code with better comments
   - Added comprehensive docstring with examples
   - Simplified union type handling with list comprehension

Benefits:
- More maintainable: type mappings in one place
- Less fragile: no scattered hardcoded values
- Easier to extend: just add to JSON_SCHEMA_TO_PYTHON_TYPE
- Better documented: clear examples in docstring
- More consistent: uses same pattern as other module constants

All tests pass (40/40 in TestTool).
@Zoe14 Zoe14 marked this pull request as ready for review November 20, 2025 20:21
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.

2 participants