Files
semantic-kernel/python/samples
Christian GlessnerandEvan Mattson 0e7556e112 Python: Add file handling support to BinaryContent for OpenAI Responses API (#12258)
## Summary

Enhances `BinaryContent` to support file handling for OpenAI Responses
API, enabling file uploads through the responses agent while maintaining
a provider-agnostic design.

  ## Changes

  ### BinaryContent Enhancements
- **Add `can_read` property**: Indicates whether content has readable
data available
- **Add `from_file()` class method**: Creates BinaryContent instances
from file paths with automatic base64 encoding
- **Fix Unicode handling**: Prevents decode errors when processing
binary files (PDFs, images, etc.)

  ### OpenAI Responses Agent Integration
- **Add BinaryContent support**: Pattern matching case for file handling
in `responses_agent_thread_actions.py`
- **Correct OpenAI API format**: Uses proper `filename` and `file_data`
structure with data URI format
- **UUID-based filenames**: Generates appropriate filenames with
mime-type extensions
- **Provider-specific mapping**: File format conversion happens only in
OpenAI agent code

  ### Testing
- **Test coverage**: New tests for `can_read`, `from_file()`, and binary
data handling
- **Unicode error prevention**: Specific test for binary PDF-like
content
- **Base64 encoding verification**: Ensures proper data format for API
compatibility

  ## Design Principles

- **Provider-agnostic**: BinaryContent remains completely generic with
no OpenAI-specific dependencies
- **Clean separation**: OpenAI format mapping isolated to OpenAI agent
files
- **No FileContent class**: Enhances existing BinaryContent instead of
introducing new types
- **Follows existing patterns**: Similar approach to ImageContent and
TextContent handling

  ## Usage Example

  ```python
 response = await self.agent.get_response(
        messages=ChatMessageContent(
            content="Analyse PDF",
            role=AuthorRole("user"),
            items=[BinaryContent.from_file(file_path=c":/test.pdf")],
        )
    )

Co-authored-by: Evan Mattson <[email protected]>
2025-06-09 21:15:39 +00:00
..

Semantic Kernel Samples

Type Description
getting_started Take this step by step tutorial to get started with Semantic Kernel and get introduced to the key concepts.
getting_started_with_agents Take this step by step tutorial to get started with Semantic Kernel Agents and get introduced to the key concepts.
getting_started_with_processes Take this step by step tutorial to get started with Semantic Kernel Processes and get introduced to the key concepts.
concepts This section contains focused samples which illustrate all of the concepts included in Semantic Kernel.
demos Look here to find a sample which demonstrate how to use many of Semantic Kernel features.
learn_resources Code snippets that are related to online documentation sources like Microsoft Learn, DevBlogs and others