LLM/OpenAI/ChatGPT Fails to Follow Character Length Requirement

I have a project where I need to generate text with a specific character length, but the model(using “langchain” gem) is sometimes generating responses that are either too short or too long.

Sample prompt I used:

**Task Objective:** Generate unique titles in different locales where each title starts with the #{top_keyword} translated into the locale's language, and ensure the titles are distinct across all locales. The titles must strictly follow length and structure constraints while ensuring uniqueness across all locales.

    **Main Keyword in Locale Language:** The title must start with the main keyword (top_keyword) translated into the respective locale's language. For example, if the top_keyword is "Basketball", the corresponding translation in the various locales should be used (e.g., "Basketball" in English, "كرة السلة" in Arabic, etc.).
    **Character Length:** Each title must be strictly between 25 and 30 characters, including spaces.
    **Uniqueness Across Locales:** Ensure that each title is unique across all locales. Titles in different locales must not repeat or be similar to one another, even if they contain the same main keyword.
    **Locale-Specific Titles:**  Generate titles for the following locales, ensuring that they are optimized for each locale’s language and context:
        en-US
        ar-SA
        es-MX
        ko (Korean)
        pt-BR (Brazilian Portuguese)
        ru (Russian)
        vi (Vietnamese)
        zh-Hans (Simplified Chinese)

    **Output Format:** Return an array of title objects with the locale and the corresponding title:
    [
        { "locale": "en-US", "title": "Top keyword - Best 2024" },
        { "locale": "ar-SA", "title": "أفضل تطبيق بـ Top keyword" },
        ...
    ]
[
    { "locale": "en-US", "title": "Basketball Best Plays of 2024" },
    { "locale": "ar-SA", "title": "كرة السلة لحظات لا تنسى 2024" },
    { "locale": "es-MX", "title": "Baloncesto Momentos Épicos 2024" },
    { "locale": "ko", "title": "농구 최고 장면 2024" },
    { "locale": "pt-BR", "title": "Basquete Jogo Incrível 2024" },
    { "locale": "ru", "title": "Баскетбол Лучшие игры 2024" },
    { "locale": "vi", "title": "Bóng Rổ Cực Đỉnh 2024" },
    { "locale": "zh-Hans", "title": "篮球 精彩时刻 2024" }
]

I have tried both the default gpt-3.5-turbo and gpt-4 models.

How can I enforce a strict character length on the output of OpenAI’s models? Any help or suggestions would be greatly appreciated!