Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Monty

 Monty

A 5 minutes lighting talk about Pydantic Monty I shared to Tokyo Python Meetup hosted by Ben.

These days I prefer give short talk like this, it’s mainly to keep my hacker spirit in the AI era.

Avatar for note35

note35

May 20, 2026

More Decks by note35

Other Decks in Programming

Transcript

  1. 1

  2. 3

  3. A brilliant idea? 1. Fast (in Rust) 2. Freedom (don't

    get bothered by CPython development) 3. Safe Prompt Request LLM Generated Python Code Monty (Python Interpreter in Rust) Prompt Response Monty's Limitation (as of 2026 May) 1. Use the rest of the standard library 2. Use third party libraries (like Pydantic) 3. Define classes (to-be-supported) 4. Use match statements (to-be-supported) 5
  4. An example: LLM's code import pydantic_monty code = """ async

    def agent(prompt: str, messages: Messages): while True: print(f'messages so far: {messages}') output = await call_llm(prompt, messages) if isinstance(output, str): return output messages.extend(output) await agent(prompt, []) """ type_definitions = """...""" m = pydantic_monty.Monty( code, inputs=['prompt'], script_name='agent.py', type_check=True, type_check_stubs=type_definitions, ) async def call_llm(prompt, messages): if len(messages) < 2: return [{'role': 'system', 'content': 'resp'}] else: return f'example output, message count {len(messages)}' async def main(): output = await m.run_async( inputs={'prompt': 'testing'}, external_functions={'call_llm': call_llm}, ) print(output) #> example output, message count 2 if __name__ == '__main__': import asyncio asyncio.run(main()) 6
  5. An example: Monty import pydantic_monty code = """ async def

    agent(prompt: str, messages: Messages): while True: print(f'messages so far: {messages}') output = await call_llm(prompt, messages) if isinstance(output, str): return output messages.extend(output) await agent(prompt, []) """ type_definitions = """...""" m = pydantic_monty.Monty( code, inputs=['prompt'], script_name='agent.py', type_check=True, type_check_stubs=type_definitions, ) async def call_llm(prompt, messages): if len(messages) < 2: return [{'role': 'system', 'content': 'resp'}] else: return f'example output, message count {len(messages)}' async def main(): output = await m.run_async( inputs={'prompt': 'testing'}, external_functions={'call_llm': call_llm}, ) print(output) #> example output, message count 2 if __name__ == '__main__': import asyncio asyncio.run(main()) 7
  6. An example: Python's code import pydantic_monty code = """ async

    def agent(prompt: str, messages: Messages): while True: print(f'messages so far: {messages}') output = await call_llm(prompt, messages) if isinstance(output, str): return output messages.extend(output) await agent(prompt, []) """ type_definitions = """...""" m = pydantic_monty.Monty( code, inputs=['prompt'], script_name='agent.py', type_check=True, type_check_stubs=type_definitions, ) async def call_llm(prompt, messages): if len(messages) < 2: return [{'role': 'system', 'content': 'resp'}] else: return f'example output, message count {len(messages)}' async def main(): output = await m.run_async( inputs={'prompt': 'testing'}, external_functions={'call_llm': call_llm}, ) print(output) #> example output, message count 2 if __name__ == '__main__': import asyncio asyncio.run(main()) 8
  7. An example: Run it $ pip install pydantic-monty # add

    ex1.py $ python ex1.py messages so far: [] messages so far: [{'role': 'system', 'content': 'example response'}] messages so far: [{'role': 'system', 'content': 'example response'}, {'role': 'system', 'content': 'example response'}] example output, message count 2 So it's still executed by CPython? (In fact, it can be called from also Rust and TS/JS) 9
  8. A brilliant idea? 1. Fast (in Rust) 2. Freedom (don't

    get bothered by CPython development) 3. Safe That's what it said. 󰤇 10
  9. The Value of Monty (for now) - A Nice Playground

    Features 1. Iterative Execution: start() and resume(). 2. Serialization: can be serialized to bytes and restored later. Sandbox • Application-level sandbox + Easy file mounting 14