> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sudoapp.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Image Input

> Working with images and multimodal inputs

Image input capabilities allow you to send images to vision-enabled AI models for analysis, description, and understanding.

## Basic Image Input

### Image from URL

<CodeGroup dropdown>
  ```typescript TypeScript theme={null}
  import { Sudo } from "sudo-ai";

  const sudo = new Sudo({
    serverURL: "https://sudoapp.dev/api",
    apiKey: process.env.SUDO_API_KEY ?? "",
  });

  async function analyzeImageFromUrl() {
    try {
      const response = await sudo.router.create({
        model: "gpt-4o",  // Vision-capable model
        messages: [
          {
            role: "user",
            content: [
              {
                type: "text",
                text: "What do you see in this image?"
              },
              {
                type: "image_url",
                imageUrl: {
                  url: "https://example.com/image.jpg"
                }
              }
            ]
          }
        ]
      });
      
      console.log(response.choices[0].message.content);
    } catch (error) {
      console.error("Image analysis error:", error);
    }
  }

  analyzeImageFromUrl();
  ```

  ```python Python theme={null}
  import os
  from sudo import Sudo

  with Sudo(
      server_url="https://sudoapp.dev/api",
      api_key=os.getenv("SUDO_API_KEY"),
  ) as client:
      response = client.router.create(
          model="gpt-4o",  # Vision-capable model
          messages=[
              {
                  "role": "user",
                  "content": [
                      {
                          "type": "text",
                          "text": "What do you see in this image?"
                      },
                      {
                          "type": "image_url",
                          "image_url": {
                              "url": "https://example.com/image.jpg"
                          }
                      }
                  ]
              }
          ]
      )
      
      print(response.choices[0].message.content)
  ```
</CodeGroup>

### Image from Local File

<CodeGroup dropdown>
  ```typescript TypeScript theme={null}
  import { Sudo } from "sudo-ai";
  import * as fs from 'fs';
  import * as path from 'path';

  const sudo = new Sudo({
    serverURL: "https://sudoapp.dev/api",
    apiKey: process.env.SUDO_API_KEY ?? "",
  });

  function encodeImageToBase64(imagePath: string): string {
    try {
      const imageBuffer = fs.readFileSync(imagePath);
      return imageBuffer.toString('base64');
    } catch (error) {
      throw new Error(`Failed to read image file: ${error}`);
    }
  }

  async function analyzeLocalImage(imagePath: string, prompt: string = "Describe this image"): Promise<string | null> {
    try {
      // Encode image to base64
      const base64Image = encodeImageToBase64(imagePath);
      
      // Determine image type from file extension
      const ext = path.extname(imagePath).toLowerCase();
      const mimeType = ext === '.png' ? 'image/png' : 
                      ext === '.webp' ? 'image/webp' : 
                      'image/jpeg';
      
      const response = await sudo.router.create({
        model: "gpt-4o",
        messages: [
          {
            role: "user",
            content: [
              {
                type: "text",
                text: prompt
              },
              {
                type: "image_url",
                imageUrl: {
                  url: `data:${mimeType};base64,${base64Image}`
                }
              }
            ]
          }
        ]
      });
      
      return response.choices[0].message.content;
    } catch (error) {
      console.error("Local image analysis error:", error);
      return null;
    }
  }

  // Usage
  async function example() {
    const description = await analyzeLocalImage("path/to/your/image.jpg", "What objects can you identify in this image?");
    if (description) {
      console.log(description);
    }
  }

  example();
  ```

  ```python Python theme={null}
  import os
  import base64
  from sudo import Sudo

  def encode_image_to_base64(image_path):
      """Encode local image to base64"""
      with open(image_path, "rb") as image_file:
          return base64.b64encode(image_file.read()).decode('utf-8')

  def analyze_local_image(image_path, prompt="Describe this image"):
      """Analyze a local image file"""
      # Encode image to base64
      base64_image = encode_image_to_base64(image_path)
      
      with Sudo(
          server_url="https://sudoapp.dev/api",
          api_key=os.getenv("SUDO_API_KEY"),
      ) as client:
          response = client.router.create(
              model="gpt-4o",
              messages=[
                  {
                      "role": "user",
                      "content": [
                          {
                              "type": "text",
                              "text": prompt
                          },
                          {
                              "type": "image_url",
                              "image_url": {
                                  "url": f"data:image/jpeg;base64,{base64_image}"
                              }
                          }
                      ]
                  }
              ]
          )
          
          return response.choices[0].message.content

  # Usage
  description = analyze_local_image("path/to/your/image.jpg", "What objects can you identify in this image?")
  print(description)
  ```
</CodeGroup>

## Advanced Image Analysis

### Multiple Images

<CodeGroup dropdown>
  ```typescript TypeScript theme={null}
  import { Sudo } from "sudo-ai";
  import * as fs from 'fs';
  import * as path from 'path';

  const sudo = new Sudo({
    serverURL: "https://sudoapp.dev/api",
    apiKey: process.env.SUDO_API_KEY ?? "",
  });

  function encodeImage(imagePath: string): string {
    const imageBuffer = fs.readFileSync(imagePath);
    return imageBuffer.toString('base64');
  }

  function getMimeType(imagePath: string): string {
    const ext = path.extname(imagePath).toLowerCase();
    switch (ext) {
      case '.png': return 'image/png';
      case '.webp': return 'image/webp';
      case '.gif': return 'image/gif';
      default: return 'image/jpeg';
    }
  }

  async function compareImages(imagePaths: string[], prompt: string = "Compare these images"): Promise<string | null> {
    try {
      // Build content with text and multiple images
      const content: any[] = [{ type: "text", text: prompt }];
      
      for (const imagePath of imagePaths) {
        const base64Image = encodeImage(imagePath);
        const mimeType = getMimeType(imagePath);
        
        content.push({
          type: "image_url",
          imageUrl: {
            url: `data:${mimeType};base64,${base64Image}`
          }
        });
      }
      
      const response = await sudo.router.create({
        model: "gpt-4o",
        messages: [{ role: "user", content: content }]
      });
      
      return response.choices[0].message.content;
    } catch (error) {
      console.error("Image comparison error:", error);
      return null;
    }
  }

  // Compare two images
  async function imageComparisonExample() {
    const comparison = await compareImages(
      ["image1.jpg", "image2.jpg"],
      "What are the main differences between these two images?"
    );
    
    if (comparison) {
      console.log(comparison);
    }
  }

  imageComparisonExample();
  ```

  ```python Python theme={null}
  import os
  import base64
  from sudo import Sudo

  def compare_images(image_paths, prompt="Compare these images"):
      """Compare multiple images"""
      
      def encode_image(path):
          with open(path, "rb") as f:
              return base64.b64encode(f.read()).decode('utf-8')
      
      # Build content with text and multiple images
      content = [{"type": "text", "text": prompt}]
      
      for i, path in enumerate(image_paths):
          base64_image = encode_image(path)
          content.append({
              "type": "image_url",
              "image_url": {
                  "url": f"data:image/jpeg;base64,{base64_image}"
              }
          })
      
      with Sudo(
          server_url="https://sudoapp.dev/api",
          api_key=os.getenv("SUDO_API_KEY"),
      ) as client:
          response = client.router.create(
              model="gpt-4o",
              messages=[{"role": "user", "content": content}]
          )
          
          return response.choices[0].message.content

  # Compare two images
  comparison = compare_images(
      ["image1.jpg", "image2.jpg"],
      "What are the main differences between these two images?"
  )
  print(comparison)
  ```
</CodeGroup>

## Next Steps

1. **[Reasoning](/sdk/sudo-sdk/reasoning)** - Advanced reasoning capabilities
2. **[CRUD Operations](/sdk/sudo-sdk/crud-operations)** - Manage stored completions

<Note>
  Image input requires vision-capable models like GPT-4o, Claude Sonnet-4, or Gemini Pro Vision. Ensure your images are in supported formats (JPEG, PNG, WebP). Both TypeScript and Python SDKs provide comprehensive support for image analysis with proper error handling and type safety.
</Note>
