Skip to content

OpenAI tool-format adapter & middleware #55

@dgenio

Description

@dgenio

Parent: #40 | Split: OpenAI adapter

Problem

This is part 1 of #40. Developers using OpenAI's API must manually translate between Capability objects and OpenAI's {type: "function", name, description, parameters} tool schema. This is tedious and blocks adoption.

Proposed Change

1. Schema adapter (src/agent_kernel/adapters/openai.py)

def capabilities_to_tools(caps: list[Capability]) -> list[dict]:
    """Convert Capabilities to OpenAI function tool definitions."""

def tool_call_to_request(tool_call: dict) -> CapabilityRequest:
    """Convert an OpenAI tool_call to a CapabilityRequest."""

def format_result(result: Any) -> dict:
    """Format kernel invoke() result as OpenAI function_call_output."""

2. Middleware class

class OpenAIMiddleware:
    def __init__(self, kernel: Kernel, principal: Principal): ...
    def get_tools(self) -> list[dict]: ...
    async def handle_tool_calls(self, response_output: list[dict]) -> list[dict]: ...
    def intercept_tool_call(self, callback: Callable) -> None: ...
    def intercept_tool_result(self, callback: Callable) -> None: ...

3. Namespace mapping

Map dot-notation capability IDs to OpenAI namespace format:

  • billing.list_invoices → namespace billing, function list_invoices

4. No SDK dependency

Adapters work with plain dicts — no dependency on openai package.

Acceptance Criteria

  • Round-trip: Capability → OpenAI schema → tool_callCapabilityRequest preserves all fields
  • OpenAIMiddleware.handle_tool_calls() processes through full kernel pipeline
  • intercept_tool_call() and intercept_tool_result() hooks execute correctly
  • Namespace mapping for dot-notation capability IDs
  • Schema matches current OpenAI API spec (Responses + Chat Completions)
  • No runtime dependency on openai package

Affected Files

  • src/agent_kernel/adapters/__init__.py (new)
  • src/agent_kernel/adapters/openai.py (new)
  • tests/test_adapters.py (new)
  • docs/integrations.md (usage examples)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions