Morph LogoMorph
Back to the main blog

What Data Should I Send to Morph?

A guide to formatting your inputs for Morph's code update API

Posted by Tejas Bhakta

3 minute read


What Data Should I Send to Morph?

Morph is designed to be simple yet powerful. You only need to provide two things:

  1. The original code file
  2. The update snippet from your AI assistant (like Claude or GPT-4o)

Let's break this down with examples.

The Original Code File

This is your existing code file that needs to be updated. For example:

import os

def calculate_total(items):
    total = 0
    for item in items:
        total += item.price
    return total

The Update Snippet

This is the code change suggested by your AI assistant. For example, if you asked the AI to add tax calculation:

// ... existing code ...
def calculate_total(items):
    total = 0
    for item in items:
        total += item.price
    return total * 1.1  # Add 10% tax

Understanding Truncation Markers

Often, AI assistants will use truncation markers (// ... existing code ...) to indicate unchanged parts of the code. Morph understands these markers well and we train on their variations extensively:

// ... existing code ...
def calculate_total(items):
    total = 0
    for item in items:
        total += item.price
    return total * 1.1  # Add 10% tax
// ... existing code ...

Common Truncation Patterns

  1. Start of File
def first_changed():
    # changes here
// ... existing code ...
  1. Middle of File
// ... existing code ...
def changed_function():
    # changes here
// ... existing code ...
  1. End of File
// ... existing code ...
def last_changed():
    # changes here

API Example

Here's how to use Morph with Python:

from openai import OpenAI

client = OpenAI(
    api_key="your-api-key", 
    base_url="https://api.morph.dev/v1"  # Use Morph's API URL
)

def execute_query(original_code: str, update_snippet: str) -> str:
    response = client.chat.completions.create(
        model="morph-v0",
        messages=[
            {
                "role": "user", 
                "content": f"""<code>{original_code}</code>
<update>{update_snippet}</update>"""
            }
        ],
        temperature=0.1,
        max_tokens=8192
    )
    return response.choices[0].message.content

# Example usage
original = """def add(a, b):
    return a + b"""

update = """def add(a, b):
    # Add input validation
    if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
        raise TypeError("Inputs must be numbers")
    return a + b"""

result = execute_query(original, update)
print(result)

Best Practices

  1. Keep Full Original Code: Include necessary imports and dependencies in your original code.
  2. Clear Truncation: Use standard truncation markers (// ... existing code ...) when needed (Claude would do this automatically)
  3. Extra context is not needed. Morph will use the original code as a strong prior. If you feel its needed, you can send it before the code/update in a user message.

Next Steps