Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 27, 2025

📄 7% (0.07x) speedup for prepare_unconstrained_prompt in inference/core/workflows/core_steps/models/foundation/openai/v2.py

⏱️ Runtime : 113 microseconds 106 microseconds (best of 379 runs)

📝 Explanation and details

The optimization extracts the f-string interpolation f"data:image/jpeg;base64,{base64_image}" into a separate variable image_url before using it in the nested dictionary structure.

Key optimization: By pre-computing the URL string, Python avoids performing the string interpolation operation within the deeply nested data structure construction. This reduces the computational overhead during dictionary creation, as the interpreter can reference the already-computed string value rather than executing the f-string formatting inline.

Performance impact: The test results show consistent 3-19% speedups across various scenarios, with particularly strong gains for:

  • Large inputs (19.8% faster with 1000+ character strings)
  • Functions with extra kwargs (8-14% faster)
  • Unicode/multilingual content (2-9% faster)
  • Repeated function calls (5-6% faster)

This micro-optimization is especially effective because string interpolation has measurable overhead in Python, and extracting it from nested data structure creation allows the interpreter to optimize the dictionary/list construction more efficiently. The gains are most pronounced with longer base64 strings where the interpolation cost is higher.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 239 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
from typing import List

# imports
import pytest  # used for our unit tests
from inference.core.workflows.core_steps.models.foundation.openai.v2 import \
    prepare_unconstrained_prompt

# unit tests

# 1. Basic Test Cases

def test_basic_valid_input():
    """Test with typical, valid inputs."""
    base64_image = "abc123"
    prompt = "Describe the image."
    gpt_image_detail = "high"
    codeflash_output = prepare_unconstrained_prompt(base64_image, prompt, gpt_image_detail); result = codeflash_output # 822ns -> 843ns (2.49% slower)
    item = result[0]
    # Check image_url part
    image_url_part = item["content"][1]

def test_basic_empty_prompt():
    """Test with empty prompt string."""
    base64_image = "xyz789"
    prompt = ""
    gpt_image_detail = "low"
    codeflash_output = prepare_unconstrained_prompt(base64_image, prompt, gpt_image_detail); result = codeflash_output # 829ns -> 761ns (8.94% faster)

def test_basic_empty_base64():
    """Test with empty base64_image string."""
    base64_image = ""
    prompt = "Prompt"
    gpt_image_detail = "medium"
    codeflash_output = prepare_unconstrained_prompt(base64_image, prompt, gpt_image_detail); result = codeflash_output # 843ns -> 798ns (5.64% faster)

def test_basic_empty_gpt_image_detail():
    """Test with empty gpt_image_detail string."""
    base64_image = "abc"
    prompt = "Prompt"
    gpt_image_detail = ""
    codeflash_output = prepare_unconstrained_prompt(base64_image, prompt, gpt_image_detail); result = codeflash_output # 827ns -> 800ns (3.38% faster)

def test_basic_kwargs_ignored():
    """Test that extra kwargs do not affect output."""
    base64_image = "abc"
    prompt = "Prompt"
    gpt_image_detail = "detail"
    codeflash_output = prepare_unconstrained_prompt(
        base64_image, prompt, gpt_image_detail, foo="bar", baz=123
    ); result = codeflash_output # 1.25μs -> 1.14μs (8.83% faster)
    # Output should be the same as without kwargs
    expected = [
        {
            "role": "user",
            "content": [
                {"type": "text", "text": prompt},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "data:image/jpeg;base64,abc",
                        "detail": "detail",
                    },
                },
            ],
        }
    ]

# 2. Edge Test Cases

def test_edge_long_base64():
    """Test with a very long base64_image string."""
    base64_image = "A" * 1000  # 1000-character string
    prompt = "Edge test"
    gpt_image_detail = "ultra"
    codeflash_output = prepare_unconstrained_prompt(base64_image, prompt, gpt_image_detail); result = codeflash_output # 1.08μs -> 990ns (9.29% faster)

def test_edge_special_characters():
    """Test with special characters in all fields."""
    base64_image = "Zm9vYmFy+/="
    prompt = "¿Cómo estás? 你好! 🚀"
    gpt_image_detail = "détail"
    codeflash_output = prepare_unconstrained_prompt(base64_image, prompt, gpt_image_detail); result = codeflash_output # 808ns -> 805ns (0.373% faster)

def test_edge_whitespace_fields():
    """Test with whitespace in all fields."""
    base64_image = "   "
    prompt = "   "
    gpt_image_detail = "   "
    codeflash_output = prepare_unconstrained_prompt(base64_image, prompt, gpt_image_detail); result = codeflash_output # 823ns -> 768ns (7.16% faster)




def test_edge_missing_arguments():
    """Test with missing required arguments (should raise TypeError)."""
    with pytest.raises(TypeError):
        prepare_unconstrained_prompt("abc", "prompt") # 3.58μs -> 2.76μs (29.7% faster)

def test_edge_extra_arguments():
    """Test with many extra kwargs (should not affect output)."""
    codeflash_output = prepare_unconstrained_prompt("img", "prompt", "det", a=1, b=2, c=3, d=4); result = codeflash_output # 1.58μs -> 1.40μs (13.0% faster)

# 3. Large Scale Test Cases

def test_large_scale_long_prompt():
    """Test with a very long prompt string."""
    base64_image = "abc"
    prompt = "A" * 1000  # 1000-character prompt
    gpt_image_detail = "high"
    codeflash_output = prepare_unconstrained_prompt(base64_image, prompt, gpt_image_detail); result = codeflash_output # 943ns -> 901ns (4.66% faster)

def test_large_scale_all_fields_long():
    """Test with all fields at maximum reasonable length (1000 chars)."""
    base64_image = "B" * 1000
    prompt = "P" * 1000
    gpt_image_detail = "D" * 1000
    codeflash_output = prepare_unconstrained_prompt(base64_image, prompt, gpt_image_detail); result = codeflash_output # 1.17μs -> 1.09μs (6.39% faster)

def test_large_scale_multiple_calls():
    """Test repeated calls with different data to ensure no state is shared."""
    for i in range(100):
        b64 = str(i)
        prompt = f"Prompt {i}"
        detail = f"detail-{i}"
        codeflash_output = prepare_unconstrained_prompt(b64, prompt, detail); result = codeflash_output # 34.5μs -> 32.6μs (5.63% faster)

def test_large_scale_unicode():
    """Test with large unicode content in prompt and detail."""
    base64_image = "abc"
    prompt = "😀" * 500  # 500 unicode emoji
    gpt_image_detail = "🌟" * 500
    codeflash_output = prepare_unconstrained_prompt(base64_image, prompt, gpt_image_detail); result = codeflash_output # 821ns -> 750ns (9.47% faster)

def test_large_scale_distinctness():
    """Test that each call produces independent, correct results."""
    results = []
    for i in range(10):
        base64_image = f"img{i}"
        prompt = f"prompt{i}"
        detail = f"detail{i}"
        results.append(prepare_unconstrained_prompt(base64_image, prompt, detail)) # 4.12μs -> 3.90μs (5.56% faster)
    for i, result in enumerate(results):
        pass
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from typing import List

# imports
import pytest  # used for our unit tests
from inference.core.workflows.core_steps.models.foundation.openai.v2 import \
    prepare_unconstrained_prompt

# unit tests

# -------------------------------
# Basic Test Cases
# -------------------------------

def test_basic_prompt_and_image():
    # Test with typical prompt and base64 image string
    codeflash_output = prepare_unconstrained_prompt(
        base64_image="abcd1234",
        prompt="Describe this image.",
        gpt_image_detail="high"
    ); result = codeflash_output # 1.27μs -> 1.18μs (8.26% faster)
    message = result[0]
    # Check image_url content
    img_content = message["content"][1]

def test_empty_prompt():
    # Test with empty prompt string
    codeflash_output = prepare_unconstrained_prompt(
        base64_image="xyz",
        prompt="",
        gpt_image_detail="low"
    ); result = codeflash_output # 1.18μs -> 1.18μs (0.254% slower)

def test_empty_base64_image():
    # Test with empty base64_image string
    codeflash_output = prepare_unconstrained_prompt(
        base64_image="",
        prompt="A prompt",
        gpt_image_detail="auto"
    ); result = codeflash_output # 1.16μs -> 1.08μs (7.59% faster)

def test_empty_gpt_image_detail():
    # Test with empty gpt_image_detail string
    codeflash_output = prepare_unconstrained_prompt(
        base64_image="imgdata",
        prompt="Prompt",
        gpt_image_detail=""
    ); result = codeflash_output # 1.11μs -> 1.10μs (0.727% faster)

def test_kwargs_are_ignored():
    # Test that extra kwargs don't affect the output
    codeflash_output = prepare_unconstrained_prompt(
        base64_image="img",
        prompt="Prompt",
        gpt_image_detail="high",
        irrelevant="value",
        another=123
    ); result = codeflash_output # 1.43μs -> 1.25μs (14.5% faster)

# -------------------------------
# Edge Test Cases
# -------------------------------

def test_long_prompt():
    # Test with a very long prompt string
    long_prompt = "A" * 1000
    codeflash_output = prepare_unconstrained_prompt(
        base64_image="img",
        prompt=long_prompt,
        gpt_image_detail="high"
    ); result = codeflash_output # 1.17μs -> 1.09μs (7.03% faster)

def test_long_base64_image():
    # Test with a very long base64_image string
    long_b64 = "b" * 1024
    codeflash_output = prepare_unconstrained_prompt(
        base64_image=long_b64,
        prompt="Prompt",
        gpt_image_detail="high"
    ); result = codeflash_output # 1.47μs -> 1.34μs (10.2% faster)

def test_special_characters_in_prompt():
    # Test with special characters in prompt
    prompt = "Describe:\n\t☃️🚀! \" ' \\ /"
    codeflash_output = prepare_unconstrained_prompt(
        base64_image="img",
        prompt=prompt,
        gpt_image_detail="high"
    ); result = codeflash_output # 1.10μs -> 1.07μs (3.37% faster)

def test_special_characters_in_base64_image():
    # Test with special characters in base64_image (not valid base64, but function should not validate)
    b64 = "+/==?/\\"
    codeflash_output = prepare_unconstrained_prompt(
        base64_image=b64,
        prompt="Prompt",
        gpt_image_detail="high"
    ); result = codeflash_output # 1.05μs -> 1.08μs (3.05% slower)

def test_special_characters_in_gpt_image_detail():
    # Test with special characters in gpt_image_detail
    detail = "h!gh_🚀"
    codeflash_output = prepare_unconstrained_prompt(
        base64_image="img",
        prompt="Prompt",
        gpt_image_detail=detail
    ); result = codeflash_output # 1.11μs -> 995ns (11.7% faster)

def test_all_empty_strings():
    # Test with all empty strings
    codeflash_output = prepare_unconstrained_prompt(
        base64_image="",
        prompt="",
        gpt_image_detail=""
    ); result = codeflash_output # 1.04μs -> 1.02μs (1.76% faster)

def test_none_as_kwargs():
    # Test with None passed as kwargs (should be ignored)
    codeflash_output = prepare_unconstrained_prompt(
        base64_image="img",
        prompt="Prompt",
        gpt_image_detail="detail",
        foo=None
    ); result = codeflash_output # 1.29μs -> 1.20μs (7.24% faster)

def test_numeric_strings():
    # Test with numeric strings for all fields
    codeflash_output = prepare_unconstrained_prompt(
        base64_image="123456",
        prompt="7890",
        gpt_image_detail="42"
    ); result = codeflash_output # 1.04μs -> 1.01μs (3.48% faster)

# -------------------------------
# Large Scale Test Cases
# -------------------------------

def test_large_prompt_and_base64():
    # Test with large prompt and base64_image strings (close to 1000 chars)
    prompt = "p" * 999
    base64_image = "i" * 999
    codeflash_output = prepare_unconstrained_prompt(
        base64_image=base64_image,
        prompt=prompt,
        gpt_image_detail="high"
    ); result = codeflash_output # 1.42μs -> 1.38μs (2.60% faster)

def test_many_unique_inputs():
    # Test the function repeatedly with many unique inputs to check for determinism and performance
    for i in range(100):
        prompt = f"prompt_{i}"
        base64_image = f"img_{i}"
        detail = f"detail_{i}"
        codeflash_output = prepare_unconstrained_prompt(
            base64_image=base64_image,
            prompt=prompt,
            gpt_image_detail=detail
        ); result = codeflash_output # 40.0μs -> 37.8μs (5.84% faster)

def test_performance_large_inputs():
    # Test with large strings to ensure function does not crash or slow down excessively
    prompt = "A" * 1000
    base64_image = "B" * 1000
    detail = "C" * 100
    codeflash_output = prepare_unconstrained_prompt(
        base64_image=base64_image,
        prompt=prompt,
        gpt_image_detail=detail
    ); result = codeflash_output # 1.26μs -> 1.05μs (19.8% faster)

def test_unicode_and_multilingual_support():
    # Test with unicode and multilingual input
    prompt = "描述这张图片。これは画像です。Это изображение. هذا صورة."
    base64_image = "图像数据"
    detail = "详细"
    codeflash_output = prepare_unconstrained_prompt(
        base64_image=base64_image,
        prompt=prompt,
        gpt_image_detail=detail
    ); result = codeflash_output # 1.25μs -> 1.22μs (2.63% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-prepare_unconstrained_prompt-mh9rst23 and push.

Codeflash

The optimization extracts the f-string interpolation `f"data:image/jpeg;base64,{base64_image}"` into a separate variable `image_url` before using it in the nested dictionary structure.

**Key optimization:** By pre-computing the URL string, Python avoids performing the string interpolation operation within the deeply nested data structure construction. This reduces the computational overhead during dictionary creation, as the interpreter can reference the already-computed string value rather than executing the f-string formatting inline.

**Performance impact:** The test results show consistent 3-19% speedups across various scenarios, with particularly strong gains for:
- Large inputs (19.8% faster with 1000+ character strings)
- Functions with extra kwargs (8-14% faster) 
- Unicode/multilingual content (2-9% faster)
- Repeated function calls (5-6% faster)

This micro-optimization is especially effective because string interpolation has measurable overhead in Python, and extracting it from nested data structure creation allows the interpreter to optimize the dictionary/list construction more efficiently. The gains are most pronounced with longer base64 strings where the interpolation cost is higher.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 27, 2025 23:30
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash labels Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant