Skip to main content

Anthropic Claude integration for APE (AI Programmatic Execution)

Project description

ape-anthropic

Anthropic Claude integration for APE (AI Programmatic Execution).

What is ape-anthropic?

ape-anthropic bridges APE's deterministic validation layer with Anthropic's Claude tool use API. It prevents hallucinations in Claude function parameters by enforcing strict type checking and constraints before execution.

Why ape-anthropic?

Claude's tool use is powerful but unreliable:

  • Function parameters can be incorrectly formatted
  • Type mismatches cause runtime errors
  • Missing required fields break execution
  • No validation before calling your code

ape-anthropic solves this by adding APE as a validation layer:

Claude → JSON parameters → APE validation → Deterministic execution ✓

Installation

# Core package (schema conversion + execution)
pip install ape-anthropic

# With Anthropic SDK (for code generation)
pip install ape-anthropic[anthropic]

# Development dependencies
pip install ape-anthropic[dev]

Prerequisites:

  • Python >= 3.11
  • ape-lang >= 0.2.0

Test Coverage

All tests passing

  • Total tests: 49
  • Last verified via pytest discovery

See ../ape/docs/APE_TESTING_GUARANTEES.md for details on what these tests guarantee.

The test suite covers:

  • Schema conversion (APE → Claude)
  • Executor (Claude → APE runtime)
  • Utils (error formatting, validation)
  • End-to-end integration
  • Generator (NL → APE code)

To verify test counts:

pytest packages/ape-anthropic/tests --collect-only -q

Quick Start

from anthropic import Anthropic
from ape_anthropic import ApeAnthropicFunction

# 1. Create Ape task file
# calculator.ape:
# task add:
#     inputs: a: Integer, b: Integer
#     outputs: sum: Integer
#     constraints: a > 0, b > 0
#     steps: sum = a + b

# 2. Load as Claude tool
func = ApeAnthropicFunction.from_ape_file("calculator.ape", "add")

# 3. Get Claude tool schema
tool_schema = func.to_claude_tool()

# 4. Use with Claude
client = Anthropic(api_key="your-key")
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    tools=[tool_schema],
    messages=[{"role": "user", "content": "Add 5 and 3"}]
)

# 5. Execute with APE validation
if response.stop_reason == "tool_use":
    tool_use = response.content[-1]
    result = func.execute(tool_use.input)
    print(f"Result: {result}")  # 8

API Reference

Schema Conversion

ape_task_to_claude_schema(task: ApeTask) -> dict

Converts APE task to Claude tool schema.

from ape_anthropic import ape_task_to_claude_schema, ApeTask

task = ApeTask(
    name="calculate_tax",
    inputs={"amount": "float", "rate": "float"},
    output="float",
    description="Calculate tax amount"
)

schema = ape_task_to_claude_schema(task)
# {
#     "name": "calculate_tax",
#     "description": "Calculate tax amount",
#     "input_schema": {
#         "type": "object",
#         "properties": {
#             "amount": {"type": "number"},
#             "rate": {"type": "number"}
#         },
#         "required": ["amount", "rate"]
#     }
# }

Execution

execute_claude_call(module: ApeModule, function_name: str, input_dict: dict) -> Any

Executes Claude tool use with APE validation.

from ape import compile
from ape_anthropic import execute_claude_call

module = compile("calculator.ape")
result = execute_claude_call(module, "add", {"a": 5, "b": 3})
# 8

class ApeAnthropicFunction

High-level wrapper for Ape → Claude integration.

Methods:

  • from_ape_file(ape_file, function_name) - Load from .ape file
  • to_claude_tool() - Get Claude tool schema
  • execute(input_dict) - Execute with validation

Code Generation (Experimental)

generate_ape_from_nl(prompt: str, model: str = "claude-3-5-sonnet-20241022") -> str

Generate Ape code from natural language using Claude.

Requires: pip install ape-anthropic[anthropic]

from ape_anthropic import generate_ape_from_nl

code = generate_ape_from_nl("Create a task that validates email addresses")
print(code)

Features

Type Safety

APE validates all parameters before execution:

  • Type checking: str → string, int → integer, float → number
  • Required fields: Missing parameters rejected
  • Constraint validation: Business rules enforced
  • Deterministic execution: No hallucinations

Claude Tool Schema Format

{
  "name": "function_name",
  "description": "Function description",
  "input_schema": {
    "type": "object",
    "properties": {
      "param": {"type": "number"}
    },
    "required": ["param"]
  }
}

Error Handling

  • JSONDecodeError: Invalid input format
  • TypeError: Parameter type mismatch
  • ApeExecutionError: Constraint violation
  • KeyError: Unknown function

Type Mapping

APE Type Claude JSON Schema
String string
Integer integer
Float number
Boolean boolean
List array
Dict object

Examples

Calculator

# calculator.ape
task multiply:
    inputs: x: Float, y: Float
    outputs: result: Float
    constraints: x >= 0, y >= 0
    steps: result = x * y

# main.py
from anthropic import Anthropic
from ape_anthropic import ApeAnthropicFunction

client = Anthropic(api_key="...")
func = ApeAnthropicFunction.from_ape_file("calculator.ape", "multiply")

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    tools=[func.to_claude_tool()],
    messages=[{"role": "user", "content": "Multiply 4 and 7"}]
)

if response.stop_reason == "tool_use":
    result = func.execute(response.content[-1].input)
    print(result)  # 28.0

Multi-Tool Agent

from ape_anthropic import ApeAnthropicFunction

# Load multiple tools
tools = [
    ApeAnthropicFunction.from_ape_file("math.ape", "add"),
    ApeAnthropicFunction.from_ape_file("math.ape", "subtract"),
    ApeAnthropicFunction.from_ape_file("string.ape", "reverse")
]

# Convert to Claude schemas
tool_schemas = [t.to_claude_tool() for t in tools]

# Execute based on Claude's choice
for tool_use in response.content:
    if hasattr(tool_use, 'name'):
        func = next(t for t in tools if t.function_name == tool_use.name)
        result = func.execute(tool_use.input)

Architecture

┌─────────────────────────────────────────────────┐
│              Anthropic Claude API                │
└─────────────────┬───────────────────────────────┘
                  │ tool_use response
                  ▼
         ┌────────────────────┐
         │  ape-anthropic     │
         │  - Schema converter │
         │  - Input validator  │
         │  - Executor         │
         └────────┬───────────┘
                  │ validated params
                  ▼
         ┌────────────────────┐
         │   APE Runtime      │
         │  - Type checking   │
         │  - Constraints     │
         │  - Execution       │
         └────────────────────┘

Comparison: Claude vs OpenAI

Feature Claude OpenAI
Tool format input_schema parameters
Tool ID Required Optional
Streaming Yes Yes
Max tools No limit No limit
ape-anthropic Use ape-openai

License

MIT

Links

Contributing

Contributions welcome! Please open issues or PRs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ape_anthropic-1.0.4.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ape_anthropic-1.0.4-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file ape_anthropic-1.0.4.tar.gz.

File metadata

  • Download URL: ape_anthropic-1.0.4.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for ape_anthropic-1.0.4.tar.gz
Algorithm Hash digest
SHA256 a6db794116cc69abadb28155b97ec9cecbbb2119bce4616b847f52d22e62c259
MD5 79c36b93b6c371a4553aac4a4f127759
BLAKE2b-256 0351a547f26e3f3a1eef7b7265049d0cbb30128eb2d41d3be8ee9c1647e0e256

See more details on using hashes here.

File details

Details for the file ape_anthropic-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: ape_anthropic-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for ape_anthropic-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a93d2aa26bc26988927d5e3c375bdd8e691cbc98f2309670ba5331c0a8628f4c
MD5 5442bedf2e42823cd01b4a1ef2bb2981
BLAKE2b-256 1432a68a07b3faa6dd0dd0565fd02c836c24c26a2472b4a6d96c5f3b6ff9c703

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page