In 1950, British actress Gertrude Lawrence's business manager and attorney, Fanny Holtzmann, was looking for a new vehicle for her client when the 1944 Margaret Landon novel Anna and the King of Siam (a fictionalized version of Leonowens' experiences) was sent to her by Landon's agent. According to Rodgers biographer Meryle Secrest, Holtzmann was worried that Lawrence's career was fading. The 51-year-old actress had appeared only in plays, not in musicals, since Lady in the Dark closed in 1943. Holtzmann agreed that a musical based on Anna and the King of Siam would be ideal for her client, who purchased the rights to adapt the novel for the stage. Holtzmann initially wanted Cole Porter to write the score, but he declined. She was going to approach Noël Coward next, but happened to meet Dorothy Hammerstein (Oscar's wife) in Manhattan. Holtzmann told Dorothy Hammerstein that she wanted Rodgers and Hammerstein to create a show for Lawrence, and asked her to see that her husband read a book that Holtzmann would send over. In fact, both Dorothy Rodgers and Dorothy Hammerstein had read the novel in 1944 and had urged their husbands to consider it as a possible subject for a musical.
Rodgers and Hammerstein had disliked Landon's novel as a basis for a musical when it was published, and their views still held. It consists of vignettes of life at the Siamese court, interspersed with descriptions of historical events unconnected with each other, except that the King creates most of the difficulties in the episodes, and Anna tries to resolve them. Rodgers and Hammerstein could see no coherent story from which a musical could be made until they saw the 1946 film adaptation, starring Irene Dunne and Rex Harrison, and how the screenplay united the episodes in the novel. Rodgers and Hammerstein were also concerned about writing a star vehicle. They had preferred to make stars rather than hire them, and engaging the legendary Gertrude Lawrence would be expensive. Lawrence's voice was also a worry: her limited vocal range was diminishing with the years, while her tendency to sing flat was increasing. Lawrence's temperament was another concern: though she could not sing like one, the star was known to be capable of diva-like behavior. In spite of this, they admired her acting – what Hammerstein called her "magic light", a compelling presence on stage – and agreed to write the show. For her part, Lawrence committed to remaining in the show until June 1, 1953, and waived the star's usual veto rights over cast and director, leaving control in the hands of the two authors.
Hammerstein found his "door in" to the play in Landon's account of a slave in Siam writing about Abraham Lincoln. This would eventually become the narrated dance, "The Small House of Uncle Thomas". Since a frank expression of romantic feelings between the King and Anna would be inappropriate in view of both parties' upbringing and prevailing social mores, Hammerstein wrote love scenes for a secondary couple, Tuptim, a junior wife of the King, and Lun Tha, a scholar. In the Landon work, the relationship is between Tuptim and a priest, and is not romantic. The musical's most radical change from the novel was to have the King die at the end of the play. Also, since Lawrence was not primarily a singer, the secondary couple gave Rodgers a chance to write his usual "soaring" romantic melodies. In an interview for The New York Times, Hammerstein indicated that he wrote the first scene before leaving for London and the West End production of Carousel in mid-1950; he wrote a second scene while in the British capital.
The pair had to overcome the challenge of how to represent Thai speech and music. Rodgers, who had experimented with Asian music in his short-lived 1928 musical with Lorenz Hart titled Chee-chee, did not wish to use actual Thai music, which American audiences might not find accessible. Instead, he gave his music an exotic flavor, using open fifths and chords in unusual keys, in ways pleasant to Western ears. Hammerstein faced the problem of how to represent Thai speech; he and Rodgers chose to convey it by musical sounds, made by the orchestra. For the King's style of speech, Hammerstein developed an abrupt, emphatic way of talking, which was mostly free of articles, as are many Oriental languages. The forceful style reflected the King's personality and was maintained even when he sang, especially in his one solo, "A Puzzlement". Many of the King's lines, including his first utterance, "Who? Who? Who?", and much of the initial scene between him and Anna, are drawn from Landon's version. Nevertheless, the King is presented more sympathetically in the musical than in the novel or the 1946 film, as the musical omits the torture and burning at the stake of Lady Tuptim and her partner.
With Rodgers laid up with back trouble, Hammerstein completed most of the musical's book before many songs were set to music. Early on, Hammerstein contacted set designer Jo Mielziner and costume designer Irene Sharaff and asked them to begin work in coordination with each other. Sharaff communicated with Jim Thompson, an American who had revived the Thai silk industry after World War II. Thompson sent Sharaff samples of silk cloth from Thailand and pictures of local dress from the mid-19th century. One such picture, of a Thai woman in western dress, inspired the song "Western People Funny", sung by the King's chief wife, Lady Thiang, while dressed in western garb.
Producer Leland Hayward, who had worked with the duo on South Pacific, approached Jerome Robbins to choreograph a ballet for "The Small House of Uncle Thomas". Robbins was very enthusiastic about the project and asked to choreograph the other musical numbers as well, although Rodgers and Hammerstein had originally planned little other dancing. Robbins staged "The Small House of Uncle Thomas" as an intimate performance, rather than a large production number. His choreography for the parade of the King's children to meet their teacher ("March of the Royal Siamese Children") drew great acclaim. Robert Russell Bennett provided the orchestrations, and Trude Rittman arranged the ballet music.
The pair discussed having an Act 1 musical scene involving Anna and the King's wives. The lyrics for that scene proved to be very difficult for Hammerstein to write. He first thought that Anna would simply tell the wives something about her past, and wrote such lyrics as "I was dazzled by the splendor/Of Calcutta and Bombay" and "The celebrities were many/And the parties very gay/(I recall a curry dinner/And a certain Major Grey)." Eventually, Hammerstein decided to write about how Anna felt, a song which would not only explain her past and her motivation for traveling with her son to the court of Siam, but also serve to establish a bond with Tuptim and lay the groundwork for the conflict that devastates Anna's relationship with the King. "Hello, Young Lovers", the resulting song, was the work of five exhausting weeks for Hammerstein. He finally sent the lyrics to Rodgers by messenger and awaited his reaction. Hammerstein considered the song his best work and was anxious to hear what Rodgers thought of it, but no comment came from Rodgers. Pride kept Hammerstein from asking. Finally, after four days, the two happened to be talking on the phone about other matters, and at the end of the conversation, Rodgers stated, very briefly, that the lyric was fine. Josh Logan, who had worked closely with Hammerstein on South Pacific, listened to the usually unflappable writer pour out his unhappy feelings. It was one of the few times that Hammerstein and Rodgers did not display a united front.