Back to Integrations
Integration/Python SDK

Python SDK Integration

Direct Python integration for maximum flexibility and control.

Category
SDKs
Difficulty
Beginner
Tags
3 features
Setup Time
< 5 min
Python
Direct Integration
Full Control

Quick Start Guide

Installation

Terminal
pip install cognitora openai

Implementation

Python
python
1import os
2from cognitora import Cognitora
3
4# Initialize client
5cognitora = Cognitora(
6    api_key=os.getenv("COGNITORA_API_KEY"),
7    base_url="https://api.cognitora.dev"
8)
9
10class CodeInterpreter:
11    def __init__(self):
12        self.session = cognitora.code_interpreter.create_session(
13            language="python",
14            timeout_minutes=30,
15            resources={
16                "cpu_cores": 1.0,
17                "memory_mb": 512,
18                "storage_gb": 5
19            }
20        )
21        
22    def execute_code(self, code: str, language: str = "python") -> dict:
23        try:
24            execution = cognitora.code_interpreter.execute(
25                code=code,
26                language=language,
27                session_id=self.session.session_id,
28                timeout_seconds=30
29            )
30            
31            return {
32                "success": execution.data.status == "completed",
33                "outputs": execution.data.outputs,
34                "execution_time_ms": execution.data.execution_time_ms,
35                "session_id": execution.data.session_id
36            }
37        except Exception as e:
38            return {
39                "success": False,
40                "error": str(e),
41                "outputs": [],
42                "execution_time_ms": 0
43            }
44
45    def execute_compute(self, image: str, command: list, **kwargs) -> dict:
46        """Execute code using compute API for containerized workloads"""
47        try:
48            execution = cognitora.compute.create_execution(
49                image=image,
50                command=command,
51                cpu_cores=kwargs.get('cpu_cores', 1.0),
52                memory_mb=kwargs.get('memory_mb', 512),
53                max_cost_credits=kwargs.get('max_cost_credits', 10),
54                storage_gb=kwargs.get('storage_gb', 5)
55            )
56            
57            return {
58                "execution_id": execution.id,
59                "status": execution.status,
60                "image": execution.image,
61                "command": execution.command
62            }
63        except Exception as e:
64            return {
65                "success": False,
66                "error": str(e)
67            }
68
69# Example usage
70interpreter = CodeInterpreter()
71
72# Execute Python code in session
73result = interpreter.execute_code("print('Hello from Cognitora!')")
74print("Session result:", result)
75
76# Execute containerized workload
77compute_result = interpreter.execute_compute(
78    image="docker.io/library/python:3.11-slim",
79    command=["python", "-c", "print('Hello from container!')"],
80    cpu_cores=1.0,
81    memory_mb=512,
82    max_cost_credits=10
83)
84print("Compute result:", compute_result)

Ready to integrate Python SDK?

Get started with Python SDK and Cognitora in minutes. Secure, scalable, and ready for anything.