Codifying Character Logic in Role-Playing

Letian Peng, Jingbo Shang

Abstract

Traditional Large Language Model (LLM) role-playing relies on unstructured textual prompts, which often lead to character drift, poor debuggability, and inconsistent behavior. This paper introduces Codified Profiles, a framework that shifts character personas from natural language descriptions into executable Python functions. By offloading character reasoning to deterministic code and semantic condition checks, this approach significantly improves behavioral consistency. Our evaluation using the Fandom Benchmark—a new dataset of over 5,000 scenes—demonstrates that codified profiles outperform standard textual prompts and Retrieval-Augmented Generation (RAG) methods. Notably, this framework enables 1B-parameter models to achieve performance parity with 8B-parameter models using traditional prompting.

1. Introduction

Role-playing has emerged as a primary application for Large Language Models (LLMs), yet creating high-fidelity digital twins remains a challenge. The standard "prompt-only" approach, where a character's biography and traits are provided as unstructured text, is inherently brittle. Models frequently suffer from "character drift," where they lose the persona's voice during long interactions, or fail to adhere to complex, conditional logic. We propose a shift from textual profiles to Codified Profiles, treating character logic as a form of source code that can be executed, debugged, and version-controlled.

2. The Limitations of Textual Profiles

Traditional role-playing methods rely on dumping a character's biography into the system prompt. This approach faces three critical flaws:

  1. Lack of Persistence: Models often ignore specific traits or drift away from established logic during complex multi-turn interactions.
  2. Poor Debuggability: Modifying a natural language prompt to fix a specific behavioral error often introduces unintended side effects elsewhere in the persona.
  3. Inaccurate Randomness: Precise control over stochastic behaviors—such as a character who is "occasionally sarcastic"—is difficult to achieve through temperature settings or prompting alone.

3. Methodology: Codified Profiles

The core of our framework is the "compilation" of a character's natural language description into a structured Python function, typically defined as parse_by_scene(scene).

3.1 Semantic Condition Checking

Rather than relying on simple string matching, the framework utilizes a helper function: check_condition(scene, question). In this setup, the LLM acts as a discriminator, evaluating the current scene to return a boolean or unknown value for specific semantic queries (e.g., "Is the character currently being threatened?"). This offloads the "reasoning" aspect of role-playing into a classification task.

3.2 Logic-Grounded Generation

The Python function outputs a list of logic-grounded assertions based on the scene's state (e.g., "The character is suspicious and will withhold information"). These assertions serve as explicit constraints for the final response generation. Furthermore, developers can utilize standard Python libraries (such as random.choice) to ensure precise behavioral distributions that are impossible to maintain via prompting.

3.3 Iterative Refinement

Codified profiles allow for an evolution of logic similar to software development. If a model response contradicts the intended character persona—detected via Natural Language Inference (NLI)—the specific code block responsible can be identified and revised without affecting the rest of the profile.

4. The Fandom Benchmark

To evaluate this framework, we introduced the Fandom Benchmark, a comprehensive dataset for role-playing fidelity:

  • Data Source: Curated from Fandom.com across six iconic franchises, including JoJo's Bizarre Adventure, Game of Thrones, and Avatar: The Last Airbender.
  • Scale: Includes 83 distinct characters and 5,141 unique scenes.
  • Evaluation Metric: We utilize an LLM-based NLI framework to score model responses against ground-truth actions extracted from the original source material.

5. Key Results and Insights

5.1 Performance Gains

Codified Profiles achieved an average NLI score of 68.38 for main characters, significantly outperforming original textual profiles (65.98) and RAG-based methods (66.04). The framework proved particularly effective for "fickle" characters with complex or contradictory motives, such as Littlefinger from Game of Thrones, whose nuanced logic is more easily captured in nested if-then statements than in prose.

5.2 Efficiency and Model Distillation

One of the most striking findings was the performance of small models. A 1B-parameter model using codified profiles and a distilled condition checker achieved an NLI score of 60.21, approaching the performance of an 8B-parameter model using standard textual prompts (65.98). This suggests that structured logic can compensate for reduced parameter counts, making high-quality role-playing viable for local, low-latency deployments.

5.3 Precision in Stochastic Behavior

In tests involving randomized behavior (e.g., a character with a specific probability of choosing a certain action), codified profiles matched target probability distributions perfectly. In contrast, textual prompts exhibited significant biases, failing to maintain the requested randomness.

6. Limitations and Future Work

While codified profiles improve consistency, abstracting a rich biography into code risks losing some of the "flavor" of naturalistic expression. Future iterations may explore a hybrid approach, using code for logic and text for stylistic nuances. Additionally, we aim to expand this framework from individual characters to "World-level role-playing," codifying the underlying physics and societal rules of fictional settings.

7. Conclusion

By treating character personas as executable logic rather than unstructured text, we provide a more robust, debuggable, and efficient path for LLM role-playing. Codified Profiles allow developers to move beyond the limitations of prompting, enabling even small-scale models to maintain complex, consistent characters across diverse narrative contexts.

Summary updated

BibTeX
@inproceedings{peng2025codifying,
  author = {Letian Peng and Jingbo Shang},
  title = {{Codifying Character Logic in Role-Playing}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=RoVS9vmpd2},
}
Figure