Last modified: Jun 11, 2026

Install Semantic Kernel in Python

Semantic Kernel is a powerful open-source SDK from Microsoft. It helps you build AI applications. You can integrate large language models (LLMs) like GPT-4 easily. This guide shows you how to install Semantic Kernel in Python. We will cover setup, examples, and common issues.

This SDK works with Python 3.8 and above. It supports plugins, planners, and memory. You can create smart apps with minimal code. Let's start the installation process.

Prerequisites

Before installing, check your system. You need Python installed. Use python --version to verify. Also, have a package manager like pip. A code editor like VS Code helps too.

Ensure you have an API key for an LLM service. Semantic Kernel works with OpenAI and Azure OpenAI. Keep your key ready for configuration.

Step 1: Install the Package

Open your terminal or command prompt. Run the following command to install Semantic Kernel:

 
pip install semantic-kernel

This installs the core library. It also installs dependencies like numpy and aiohttp. Wait for the process to finish. You should see success messages.

To verify the installation, run this Python code:

 
import semantic_kernel as sk
print(f"Semantic Kernel version: {sk.__version__}")

Semantic Kernel version: 1.0.0

If you see a version number, installation is complete. If not, check for errors.

Step 2: Set Up Your Environment

Now, configure your LLM service. Create a Python file, e.g., app.py. Add the following code to set up Semantic Kernel with OpenAI:

 
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

# Create a kernel instance
kernel = sk.Kernel()

# Add OpenAI chat completion service
api_key = "your-openai-api-key"
org_id = None
kernel.add_chat_service("gpt-4", OpenAIChatCompletion("gpt-4", api_key, org_id))

print("Kernel setup complete!")

Replace "your-openai-api-key" with your actual key. You can also use Azure OpenAI by importing AzureChatCompletion. For Azure, you need endpoint and deployment name.

Important: Never share your API key in code. Use environment variables for security. Store the key in a .env file and load it with python-dotenv.

Step 3: Create Your First Semantic Function

Semantic functions are prompts that use LLMs. Define a simple prompt to test. Create a folder called plugins and a subfolder HelloPlugin. Inside, add a file skprompt.txt with this content:


Say hello to {{$input}} in a friendly way.

Now, create a configuration file config.json in the same folder:

 
{
    "schema": 1,
    "type": "completion",
    "description": "Greets the user",
    "completion": {
        "max_tokens": 100,
        "temperature": 0.7
    }
}

Load this plugin in your main script:

 
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

kernel = sk.Kernel()
api_key = "your-openai-api-key"
kernel.add_chat_service("gpt-4", OpenAIChatCompletion("gpt-4", api_key))

# Import the plugin
hello_plugin = kernel.import_semantic_skill_from_directory("./plugins", "HelloPlugin")

# Run the semantic function
result = hello_plugin["HelloFunction"]("Alice")
print(result)

Hello Alice! I hope you are having a great day.

You just created your first AI function. Semantic Kernel makes this process smooth.

Step 4: Use Native Functions

Native functions are regular Python methods. You can combine them with semantic functions. This gives you more control. Create a Python file MathPlugin.py:

 
from semantic_kernel.skill_definition import sk_function

class MathPlugin:
    @sk_function(
        description="Adds two numbers",
        name="Add"
    )
    def add(self, a: float, b: float) -> float:
        return a + b

Import and use it in your kernel:

 
import semantic_kernel as sk

kernel = sk.Kernel()
math_plugin = kernel.import_skill(MathPlugin(), "MathPlugin")

result = kernel.run_async(math_plugin["Add"], input_str="2 3")
print(result)

5.0

Native functions are great for calculations or data processing. They make your AI app more robust.

Step 5: Troubleshooting Common Issues

Installation problems can happen. Here are fixes for common errors.

Error: "ModuleNotFoundError: No module named 'semantic_kernel'"
This means the package is not installed. Run pip install semantic-kernel again. Use a virtual environment to avoid conflicts.

Error: "OpenAI API key not found"
Check your API key setup. Ensure you passed the correct key to add_chat_service. Use environment variables for safety.

Error: "TimeoutError"
Your network might be slow. Increase timeout in your LLM configuration. Or check your internet connection.

Error: "Version mismatch"
Update Semantic Kernel to the latest version. Run pip install --upgrade semantic-kernel. This fixes compatibility issues.

Best Practices for Installation

Use a virtual environment to isolate dependencies. Run python -m venv venv and activate it. Then install Semantic Kernel inside it.

Keep your API keys secure. Never hardcode them in scripts. Use a .env file and the python-dotenv library to load them.

Test your setup with a simple function first. This ensures everything works before building complex apps.

Conclusion

Installing Semantic Kernel in Python is straightforward. You learned to install the package, set up an LLM service, and create both semantic and native functions. The SDK opens doors to advanced AI features like planners and memory. Start building your AI application today. With Semantic Kernel, you can integrate AI seamlessly into your Python projects.